& Lt ;? Phpechopreg_replace (& quot ;/(? & Lt; href) ([^ \ & gt;] *) (? \ & Gt;)/I & quot;, & quot; # & quot;, & quot; & lt; ahref 'www .ccliving.cn & quot; & gt; hello, click here to see & lt;/a & gt; & lt; ahre
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.
PHP perfectly extracts link regular expressions
Function match_links ($ document ){
Preg_match_all ("'<\ s * a \ s .*? Href \ s * = \ s * ([\ "\ '])? (? (1 )(.*?) \ 1 | ([^ \ s \>] +) [^>] *>? (.*?) '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;
}
PHP regular expression to extract hyperlinks and their titles
There is such an HTML section, which is relatively irregular. if you want to extract the link address and link name, how can we get it?
// HTML
$ Str = 'Song list
Chinese score list
• Light Music';
Using regular expressions is the simplest. other methods, even if you want to use regular expressions, are used...
$ Pat = '/(.*?) <\/A>/I ';
Preg_match_all ($ pat, $ str, $ m );
Output method:
Print_r ($ m [2]);
Print_r ($ m [4]);
Or:
For ($ I = 0; $ I
Echo'
'. $ M [4] [$ I]. '';}
The result is as follows:
Array ([0] =>? Http://list.mp3.baidu.com/song/A.htm? Top 8? [1] =>? Http://list.mp3.baidu.com/list/bangping.html? [2] => qingyinyue.html? Top19) Array ([0] => Song list [1] => Chinese music list [2] => light music)
// Fck editor removes the compilation symbol \ when extracting the hyperlink
Class pp {
// Determine the attachment uploaded by FCK and change the compiled symbol.
Function match_links ($ document ){
Preg_match_all ("'<\ s * a \ s .*? Href \ s * = \ s * ([\ "\ '])? (? (1 )(.*?) \ 1 | ([^ \ s \>] +) [^>] *>? (.*?) '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;
}
}
Run the following command:
$ R = $ k-> match_links ($ fckvalue );
$ Wv = "";
If ($ r! = ""){
$ Pat = "'<\ s * a \ s .*? Href \ s * = \ s * ([\ "\ '])? (? (1 )(.*?) \ 1 | ([^ \ s \>] +) [^>] *>? (.*?) 'Isx ";
$ Fckvalue = preg_replace ($ pat, "< <附件> >>", $ Fckvalue );
Foreach ($ r ["all"] as $ kk => $ vv ){
$ Wv. = str_replace (array ("\"), "", $ vv );
}
}
If ($ wv! = ""){
$ Fckvalue = $ fckvalue. $ wv;
}
In this way, when you can use the FCK hyperlink to upload the PHP page, the link will become in the form of aaa.