Regexp problem in PHP

Tommienbp
Tommienbp used Ask the Experts™
on
Hello,

I've got a large area of plain HTML code. I want to filter out all the <IMG> tags which look like this:

<IMG id=64_1 onresize="parent.resize(this,'198','198')" style="WIDTH: 198px; HEIGHT: 198px" alt="" src="http://www.domain.nl/sre/data/origineel/64_1.jpg">

Of this tag I want to filter out the height, width and id and put this in a variable.

Next I want to replace the whole tag with:
<IMG id=64_1 alt="" src="http://www.domain.nl/sre/data/64_1.jpg"> and put it back in the HTML code.

I want to do this for all Image tags in the document.
Is it possible to put all image tags in an array and check/change them all?

Sorry for my bad english and thanks for any help or suggestions. Tom
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Well I use a program called Codewrite 32.

All I do is if I am replacing a GLOBAL variable where it is the same throughout the program I just hit ctrl + r It is like the find command but you enter what you are replacing and what with.  Then you can do it by each variable or check globally.

http://www.premia.com/download/
You can get a 30 day trial copy here.

However if each one of the <img... is different you might just have to replace what is the same in all of them and then go back and edit.

Not sure if this is your best solution but if they are all the same its a 3 step process.
Richard QuadlingSenior Software Developer

Commented:
The following regexp will match any image tag which contains the elements you've shown (id, onresize, alt, src)

<IMG ID=(.*) onresize=(.*) style=(.*) alt=(.*) src=(.*)>

Each sub-pattern can then be checked in turn.

You can then create a new output string of ...

<img id=\1 alt=\4 src=\5>

Richard.
Richard QuadlingSenior Software Developer

Commented:
Doing this as a one off task for all the files in a site, I would recommend a 30 day trial product (as suggested by bmarlin). The amount of time it would take to do 90% would probably be a few minutes and then the rest would be done by hand or by a more refined search/replace.

Richard.
I think the best tool for this is sed. You could upload your files to a Linux/Unix box and use the sed command (streamline editor).

Here's an example of what it can do. Give a file with this text:

<IMG id=64_1 onresize="parent.resize(this,'198','198')" style="WIDTH: 198px; HEIGHT: 198px" alt="" src="http://www.domain.nl/sre/data/origineel/64_1.jpg">

do this sed commmand:
sed 's/style\=\"WIDTH: 198px; HEIGHT: 198px\" alt=\"\"//g' data.txt

the substitute feature for sed is in this form:
s/[target text]/[replacement text]/g

[target text] can be a regular expression.

The example doesn't do exactly what you want, so you'll have to fiddle with the right expression to get what you want. Also look in the man page to see more information on how to issue multiple sed commands.


Author

Commented:
Thanks a lot, that will do the trick in the future too. :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial