2

In the following string:

<table  border="1"><tr><td class=" m" bgcolor="#cccccc" style="bold" size="7" m="m "><span></span> </td><td class=" m" bgcolor="#cccccc" style="bold" size="7" m="m " align="center">NAMES </td><td class=" m" bgcolor="#cccccc" style="bold" size="7" m="m " colspan="13" align="center">AREAS </td></tr></table>

I'd like to remove all attributes other than the following: border,bgcolor and class.

^(?:border|class|bgcolor)\b))=".*?"

doesn't work out. Any suggestions please?

Eat Ong
  • 527
  • 4
  • 7
  • You can use a regex for that. But it's only advisable for output transformation, not as security feature. A regex would be too much overhead to cover the various legal serializations. – mario Jan 19 '12 at 00:18

1 Answers1

2
preg_replace('/ (?!(border|class|bgcolor))[^=" ]+="[^"]*"/', '', $str);

See it here in action: http://regexr.com?2vp0m


That said, please don't use a regex to parse HTML!!!

Community
  • 1
  • 1
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292