1
<div style = "text-align:left;" class="ref"> Text </div>

I want to replace <div> with <p> without losing attributes.

Any help is appreciated.

sonubreeze
  • 115
  • 4
  • 14

1 Answers1

3

Try This:

$str = '<div style = "text-align:left;" class="ref"> Text </div>';

$newstr = preg_replace('/<div [^<]*?class="([^<]*?ref.*?)">(.*?)<\/div>/','<p class="$1">$2</p>',$str);

echo $newstr;

Output : <p class="ref"> Text </p>

NID
  • 3,238
  • 1
  • 17
  • 28