I write php all day but I suck at regex. I just can't wrap my head around it and get anything decent to work without spending 16 hours with scattershot techniques. I usually just end up saying screw it and writing 20 lines of code that do the same thing I know can be done with one line of preg_replace. Here's what I need and if anyone can help I would be in your debt. Imagine an arbitrary block of html like this as my string to parse: <a href="http://www.domain.com/" target="_blank"><img src="http://www.domain.com/pics/blah.gif" border="0" alt="whatever"></a><br> <a href="http://www.domain.com/" target="_blank">blahblah</a><br/><br/> <a href="http://www.domain.com/" target="_blank"><img src="http://www.blahblah.com/pics/blahblah.jpg" border="0" width="729" alt="whatever"></a><br> <a href="http://www.domain.com/" target="_blank">blahblah</a><br/><br/> Code (markup): I also have a variable set with a value: $newWidth = 300; Code (markup): What I need to do is take any image sections in that string block and if they DO have a width set inside them I need to replace the width value with $newWidth. If they DON'T have a width set then I need to add one before the closing img tag, again with the value set to the $newWidth value. So the resulting output in this case would be: <a href="http://www.domain.com/" target="_blank"><img src="http://www.domain.com/pics/blah.gif" border="0" alt="whatever" width="300"></a><br> <a href="http://www.domain.com/" target="_blank">blahblah</a><br/><br/> <a href="http://www.domain.com/" target="_blank"><img src="http://www.blahblah.com/pics/blahblah.jpg" border="0" width="300" alt="whatever"></a><br> <a href="http://www.domain.com/" target="_blank">blahblah</a><br/><br/> Code (markup): I know this can be done with one preg_replace line but my brain is about to explode trying to figure out how. Some people enclose width values in quotes and some don't so the regex has to also allow for that. If anyone can help me out here I would really really appreciate it. Thanks!
Well I would use a "max-width" style tag if it would work in IE and Firefox, but of course it doesn't work in IE. In any case I've got some people on another forum helping with this as well and it turns out that since my initial string to parse is user-submitted, using a regex might not even be the best way to go since there are so many ways people can put things in. So I'm basically going to stick with my big bunch of code that seems to parse it all just fine. I had just hoped to accomplish the same thing with shorter code.