PHP regular expression extraction href address in html hyperlink _ PHP Tutorial

Source: Internet
Author: User
Tags php regular expression preg
The PHP regular expression extracts the href address from the html hyperlink. Sometimes we need to filter or extract external links of html strings. next I will introduce an href address program that uses PHP regular expressions to extract the href address from html hyperlinks. When using php, we need to filter or extract external links of html strings. next I will introduce an href address program that uses PHP regular expressions to extract the href address from html hyperlinks.

Use php regular expression-related functions to extract addresses from html hyperlinks.

The code is as follows:

$ Preg = '// is ';

$ Str = 'urlname text segment 1URLNAME text segment 2URLNAME... text segment n ';

Preg_match_all ($ preg, $ str, $ match); // search for all matches in $ str $ preg add $ match

For ($ I = 0; $ I

{

Echo $ match [1] [$ I]."
";

}

?>

Final output:

Link 1
Link 2
Link 3


Attached
PHP regular expression to extract the image address code.

The code is as follows:

$ Str ='

';

$ Pattern = "/<[img | IMG]. *? Src = ['| "] (. *? (? : 2.16.gif |. jpg]) ['| "]. *? [/]?> /";

Preg_match_all ($ pattern, $ str, $ match );

Print_r ($ match );

Bytes. Using php's regular...

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.