Php matches the link address program code in characters. Yesterday, if a friend asked me a question, could I check whether the data submitted by the user contains a super connection? if so, I directly filtered the data, next I will introduce you to the method of filtering connections. I had a friend question yesterday. can I check whether the data submitted by the user contains a super connection? if so, I will filter it directly, the following describes how to filter connections.
Determines whether a string contains a hyperlink.
| The code is as follows: |
|
|
$ Str = "ssdsfsdfsdfss ";
If (preg_match ("/] *> | ] *>/I ", $ str ))
{
Echo "this string has a hyperlink ";
}
Else
{
Echo "this string has no hyperlink mark ";
}
?> |
Next we only need to filter the connection section.
| The code is as follows: |
|
|
Echo preg_replace ("/(? <= Href =) ([^>] *) (? =>)/I "," # "," Hello, click here to see you, click here to see ");
?> |
Regular :/(? <= Href =) ([^>] *) (? => )/
(? <= Exp) match the position behind exp
(? = Exp) match the position before exp
This regular expression matches all non-">" characters before ">" after href =
Example:
Find these characters (URLs) and replace them with # to remove all links in html.
Now I want to share an instance that extracts Super connections.
| The code is as follows: |
|
|
Function match_links ($ document ){
Preg_match_all ("' ] +) [^>] *>? (.*?) 'Isx ", $ document, $ links );
While (list ($ key, $ val) = each ($ links [2]) {
If (! Empty ($ val ))
$ Match ['link'] [] = $ val;
}
While (list ($ key, $ val) = each ($ links [3]) {
If (! Empty ($ val ))
$ Match ['link'] [] = $ val;
}
While (list ($ key, $ val) = each ($ links [4]) {
If (! Empty ($ val ))
$ Match ['content'] [] = $ val;
}
While (list ($ key, $ val) = each ($ links [0]) {
If (! Empty ($ val ))
$ Match ['all'] [] = $ val;
}
Return $ match;
} |
...