Php uses regular expressions to filter html hyperlinks and extract links

Source: Internet
Author: User
& 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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.