0

i have a problem. i can't replace and add inline style css. i have this input:

<p  style="text-align: justify;"><span style="font-size: 11pt;">hello world<br /></span></p>
<p id="tem" style="text-align: justify;">test</p>
<p >&nbsp;</p>
<p>&nbsp;</p>

and i want this output:

<p style="text-align: justify;margin:0;"><span style="font-size: 11pt;">hello world<br /></span></p>
<p id="tem" style="text-align: justify;margin:0;">test</p>
<p  style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;</p>

i have tried this way demo

 $re = "/(<p[^>]+\")([^>\"]+)/miu";
    $str = "<p id=\"tem\" style=\"text-align: justify;\"><span style=\"font-size: 11pt;\">hello world<br /></span></p>\n<p style=\"text-align: justify;\">test</p>\n<p >&nbsp;</p>\n<p>&nbsp;</p>";
    $subst = "$1$2margin:0;";

    $result = preg_replace($re, $subst, $str);

please help me anybody. thanks.

Ahosan Karim Asik
  • 3,219
  • 1
  • 18
  • 27

1 Answers1

0

I have solved that:

search by regex pattern ((?<=\<)[^>]+style=")([^>"]+) and replace with $1$2margin:0;

Live demo

Ahosan Karim Asik
  • 3,219
  • 1
  • 18
  • 27