php regex preg_replace html标签

hello I have the following code :

$content = '<div class="style1" id="divelement" align="center">';

and here is my code :

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

now what I'm trying is to replace the whole elements but keep class and id only .

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.