fork download
  1. <?php
  2.  
  3. $re = '~(?:<\w+|(?!^)\G)(?:\s+(?:src|href)=(?:"[^"]*"|\'[^\']*\'))*\s+(?!(?:href|src)=)\w+=\K(?|"([^\s"=]*)"|\'([^\s\'=]*)\')~u';
  4. $str = "<mytag src=\"src_here\" b=\"yes\" href=\"href_here\"> becomes <mytag src=\"src_here\" b=yes href=\"href_here\">\n<mytag b='yes'> becomes <mytag b=yes>\nbut <script>var b=\"yes\"</script> stays intact\n<mytag b=\"no no\" c=\"no=no\"> stays intact\n<tag href=\"something\"> text <tag src=\"dddd\"> intact";
  5. $subst = "$1";
  6. $result = preg_replace($re, $subst, $str);
  7. echo $result;
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
<mytag src="src_here" b=yes href="href_here"> becomes <mytag src="src_here" b=yes href="href_here">
<mytag b=yes> becomes <mytag b=yes>
but <script>var b="yes"</script> stays intact
<mytag b="no no" c="no=no"> stays intact
<tag href="something"> text <tag src="dddd"> intact