Php regular expression extract Image address hyperlink title implementation code

Source: Internet
Author: User

The regular expression extracts the id from the URL

The code is as follows: Copy code
Preg_match ('/http://t.sina.com.cn/(d +)/fans //', $ html, $ result); regular expression extracts the id in the URL
Echo $ result [1];


PHP regular expression to extract the image address code.

The code is as follows: Copy code
$ Str = '<p style = "padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%; "> </p> <p style =" padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%; "> </p> <p style =" padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%; "> </p> '; $ pattern = "/<[img | IMG]. *? Src = ['| "] (. *? (? : 2.16.gif |. jpg]) ['| "]. *? [/]?> /"; Preg_match_all ($ pattern, $ str, $ match); print_r ($ match );

 

PHP regular expression to extract hyperlinks and their titles

 

The code is as follows: Copy code

$ Str = '<a id = "top 8" href = "http://list.MP3.baidu.com/song/A.htm? Top8 "class =" p14 "target =" _ top "> Song list </a> <br> <a target =" _ blank "id =" bp "href =" http://list.mp3.baidu.com/list/bangping.html "class =" p14 "> Chinese score </a> <br> <td nowrap =" nowrap ">&# 8226; & nbsp; <a id = "top19" href = "qingyinyue.html? Top19 "class =" p14 "target =" _ top "> light music </a> </td> ';

Using regular expressions is the simplest. Other methods, even if you want to use regular expressions, are used...

$ Pat = '/<(.*?) Href = "(.*?) "(. *?)> (.*?) </A>/I ';
Preg_match_all ($ pat, $ str, $ m );

Output method:

Print_r ($ m [2]);
Print_r ($ m [4]);

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.