[Solved] php regex preg_replace html tags


It would better to use a library like DOMDocument to parse the HTML. But if you really have to do it with a regexp….

Don’t use | in the regexp, that matches either class or id, but not both.

preg_replace('~<div\b.*?\bclass\s*=\s*"(.*?)".*?\bid\s*=\s*"(.*?)".*>~i','<div class="$1" id="$2">', $content);

Note that this will only work if the class is to the left of the id in the DIV.

1

solved php regex preg_replace html tags