0

Possible Duplicate:
php regexp: remove all attributes from an html tag

$input = '<div style="font-size: 18px; line-height: 20px;" id="whatever">text</div>';

// ... some code here, probably regex

$desired_output = '<div id="whatever">text</div>';

How do I do the above using PHP?

Community
  • 1
  • 1
Frantisek
  • 7,485
  • 15
  • 59
  • 102
  • I don't want to remove all attributes, I only want to remove the "style" attribute! – Frantisek Mar 31 '12 at 20:40
  • There are some good examples in the duplicate question which show how to specifically remove the style tag or only remove a single attribute. – Tim M. Mar 31 '12 at 20:43
  • @RIMMER my answer in that question shows exactly that. And there is also plenty additional duplicates explaining how to remove an attribute from an element. Also have a look at http://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element/3820783#3820783 – Gordon Mar 31 '12 at 20:44

1 Answers1

1

try the following you need a regular expression to strip it out.

$desired_output = preg_replace('/<\s*style.+?<\s*\/\s*style.*?>/si', ' ', $input );

or this

$desired_output = preg_replace('%style="[^"]+"%i', '', $input);
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • And once again I find myself having to post this link. You can't parse HTML with regex. http://stackoverflow.com/a/1732454/477127 – GordonM Mar 31 '12 at 20:41
  • @GordonM: A DOM solution might be better, but using regex to extract information from HTML is a completely valid solution as long as it works. – Blender Mar 31 '12 at 20:43
  • @Blender - Except that it won't work, hence why it is an invalid solution. – Christian Mar 31 '12 at 20:48