PHP regular expression to extract hyperlinks and their titles

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

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? Top8 [1] => http://list.mp3.baidu.com/list/bangping.html [2] => qingyinyue.html? Top19) Array ([0] => Song list [1] => Chinese music list [2] => light music)

    So we need to collect the title and link address of a website... Apply it by yourself...

  • 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.