Home  >  Article  >  Backend Development  >  PHP regular search for html tags containing id attributes in html

PHP regular search for html tags containing id attributes in html

WBOY
WBOYOriginal
2016-07-25 08:52:212262browse
In php, use regular expressions to find all html tags with id attributes in html. This article gives detailed operation methods. Friends in need can refer to it.

How to use php regular expression to find tags with id attribute in html? That is, you need to find the html tags in angle brackets and id="". For example: String:

123213
21314423
Mismatch:
……
’s content/div> The above content can be matched using the following regular rules: ]+?id=[^>]+?>.*? Here is an example of a complete PHP regular match for HTML tags containing ID attributes. For example:
<?php
$str = '<div style="float:left" id="ab">123213</div><div class="a123">213123</div>';
$search = '#<[a-zA-Z0-9][^>]+?id=[^>]+?>.*?</div>#is';
preg_match_all($search,$str,$r);
echo '<pre class="brush:php;toolbar:false">';
print_r($r);
echo '
'; ?>

Output: Array ( [0] => Array ( [0] =>

123213
)

)



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn