0

I have absoulutely no experience with regular expressions, so I've run into a small issue. I need to replace all height="*" and width="*"-html-tags with height="auto" and width="auto".

What regular expression can I use to accomplish this in PHP?
Thanks!

Emil
  • 7,220
  • 17
  • 76
  • 135
  • I don't know why you would want to do this but isn't adding a style easier? * { height: auto; width: auto } – Han Dijk Mar 31 '11 at 15:28
  • [You can't parse (X)HTML with regex.](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) The fact that you have no experience isn't going to make it any better. – unholysampler Mar 31 '11 at 15:30

1 Answers1

2

Note that this is only true for the literal asterisk character. For wildcards you should the regex after the update.

You don't need regular expressions for this, just two str_replace()

$html = str_replace (' height="*"', ' height="auto"', $html);
$html = str_replace (' width="*"', ' width="auto"', $html);

Update

You can use RegEx though. It requires more resources, but some rudamentary checks can be added (so for example height="*" won't be changed when NOT inside an element):

preg_replace ('/(<[^>]+ (height|width)=)"[^"]*"/i', '$1"auto"', $html);
vbence
  • 20,084
  • 9
  • 69
  • 118