How to grab a specific attribute value of a tag with PHP regular expression
This article mainly introduces "how to use PHP regular expression to grab the specific attribute value of a tag". In daily operation, it is believed that many people have doubts about how to use PHP regular expression to grab the specific attribute value of a tag. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "how to use PHP regular expressions to grab a specific attribute value of a tag"! Next, please follow the editor to study!
Php learned regularization for a few days, grabbed some website data, and found it troublesome to write regularities every time, so he wanted to write an interface that crawls specific tags with specific attribute values, directly on the code.
/ / $html- searched string $tag- looked up tag $attr- looked up attribute name $value- looked up attribute value function get_tag_data ($html,$tag,$attr,$value) {$regex = "/ (. *?) / is"; echo $regex. "
"; preg_match_all ($regex,$html,$matches,PREG_PATTERN_ORDER); return $matches [1];} / / the return value is the contents of the tags found in the array
Here's a random example.
Header ("Content-type: text/html; charset=utf-8"); $temp = 'Home blog LOFTER photo album about me'; $result = get_tag_data ($temp, "a", "class", "fc01"); var_dump ($result)
The output is
Array (6) {[0] = > string (6) "Home" [1] = > string (6) "Log" [2] = > string (6) "LOFTER" [3] = > string (6) "Photo album" [4] = > string (6) "Boyou" [5] = > string (9) "about me"}
If you view the source code, you can see
Array (6) {[0] = > string (6) "Home" [1] = > string (6) "Log" [2] = > string (6) "LOFTER" [3] = > string (6) "Photo album" [4] = > string (6) "Bo you" [5] = > string (9) "about me"} this is the end of the study on "how to use PHP regular expression to grab a specific attribute value of a tag". Hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!