How to use regular expressions to match different types of html tag tags with multiple attributes? the order of attributes is unclear. & lt; a & nbsp; id = "home" & nbsp; href = "/Home/index" & nbsp; class = "btn" & gt; & lt; a & nbsp; href = "/Home/index" & nbsp; how to use regular expressions to match html tags with multiple attributes
There are various tags and there is no rule. The order of attributes is unclear.
For example, if the attribute order is different, there are several types of attributes, and three attributes of id href class are obtained respectively, which are saved to \ 1 \ 2 \ 3 (I use preg_replace, because you want to replace the entire tag with the specified format)
After thinking for a long time, I don't know how to write this regular expression.
I also thought about using] *)> after matching the post-processing attribute, replace it with str_replace. However, repeated searches may lead to excessive resource overhead. Is there any way to achieve the above requirements? Help me
------ Solution --------------------
We recommend that you learn how to use PHPquery. you do not need to use regular expressions for this collection.
------ Solution --------------------
This post was last edited by xuzuning at 09:09:12
$s =<<< TXT
TXT;
echo preg_replace_callback('//i', 'foo', $s);
function foo($r) {
preg_match_all('/(\w+)="(.+?)"/', $r[0], $t);
$t = array_combine($t[1], $t[2]);
ksort($t);
$s = '';
foreach($t as $k=>$v) $s .= " $k='$v'";
return "";
}