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]); |