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 will filter the data directly. Next I will introduce you to the method of filtering connections.
Determines whether a string contains a hyperlink.
| The Code is as follows: |
Copy code |
|
$ Str = "ssdsf <a target = '_ blank' href = 'HTTP: // www. bKjia. c0m/'> sdf </a> sdfss ";
If (preg_match ("/<a/s + href = [^>] *> | </[^ a] * a [^>] *>/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: |
Copy code |
|
<? Php
Echo preg_replace ("/(? <= Href =) ([^>] *) (? =>)/I "," # "," <a href = 'www .csdn.net '> hello, click here to see </a> <a href = 'www. bKjia. c0m'> hello, click here to see </a> ");
?> |
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: <a href = 'www. bKjia. c0m'>
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: |
Copy code |
|
Function match_links ($ document ){
Preg_match_all ("'<s * .*? Hrefs * = s * (["'])? (? (1 )(.*?) \ 1 | ([^ s>] +) [^>] *>? (.*?) </A> '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;
} |