How to use regular expressions to match html tags with multiple attributes

Source: Internet
Author: User
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 "";
}



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.