1

I have this html code.

 <P class=MsoNormal style='MARGIN: 0cm 0cm 10pt'><STRONG>text</P>
    <P class=MsoNormal style='MARGIN: 0cm 0cm 10pt'></P>
    <UL>
    <LI>
    <DIV class=MsoNormal style='MARGIN: 0cm 0cm 10pt'>text</DIV></LI>
    <LI>
    <DIV class=MsoNormal style='MARGIN: 0cm 0cm 10pt'>text</DIV></LI>
    <LI>
    <DIV class=MsoNormal style='MARGIN: 0cm 0cm 10pt'>text</DIV></LI>
    <LI>
    <DIV class=MsoNormal style='MARGIN: 0cm 0cm 10pt'>text</DIV></LI>
    <LI>
    <DIV class=MsoNormal style='MARGIN: 0cm 0cm 10pt'>text</DIV></LI>
    <LI>
    <DIV class=MsoNormal style='MARGIN: 0cm 0cm 10pt'>text</DIV></LI></UL>

I want remove style tag and its proprieties as well as class tag and its proprieties so the out would be :

  <P><STRONG>text</P>
    <P></P>
    <UL>
    <LI>
    <DIV>text</DIV></LI>
    <LI>
    <DIV>text</DIV></LI>
    <LI>
    <DIV>text</DIV></LI>
    <LI>
    <DIV>text</DIV></LI>
    <LI>
    <DIV>text</DIV></LI>
    <LI>
    <DIV>text</DIV></LI></UL>

Here is what i have tried but it is not working :

$html = preg_replace('/(]+) (style|class)=("|\').*?("|\'(>|\s))/img', '$1', $$html);
2
  • a) In what way is it not working? b) You can't parse HTML with regex stackoverflow.com/a/1732454/477127 Commented Jul 9, 2013 at 11:04
  • this is the error that it gives : PHP Error[8]: Undefined variable: return the html Commented Jul 9, 2013 at 11:09

1 Answer 1

1

try this:

$html = preg_replace("/(\s(class|style)[^>]+)/", "", $html);
Sign up to request clarification or add additional context in comments.

2 Comments

just tried it it gives the same error PHP Error[8]: Undefined variable:
thanks zekus works after changing $$html to $html. how can i remove if it exists the <img> tag too ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.