views:

33

answers:

1

Hi all

I want to use the php function preg_match_all to find a part of the html code to replace it by another one.

This is what I need to find:

<attachfiles>
tag{link} attr{rel="stylesheet" type="text/css" media="screen"} 
sources{
file1.css,
file2.css
}
</attachfiles>

I made a regular expression that find it but only if that code is present once into the entire html.

My regular expression is:

"|\<attachfiles\>(.*)\<\/attachfiles\>|s"

The issue comes out when I have the code to find repeated two or more times. Since the regular expression uses the |s operator (multiline), when I have the code more than one time it returns all the html code from the very first to the vary last

For example:

<attachfiles>
tag{link} attr{rel="stylesheet" type="text/css" media="screen"} 
sources{
file1.css,
file2.css
}
</attachfiles>

... html code ...
... html code ...

<attachfiles>
tag{script} attr{type="text/css" language="javascript"} 
sources{
file1.js,
file2.js
}
</attachfiles>

My regular expression in this case is returning ALL the code, from the first

<attachfiles> to the last </attachfiles> 

including the

... html code ... 
... html code ... 

that is between the code that I am searching for.

Anyone knows how to solve it???

Thanks in advance

Ivano

A: 

Use the DOM and create a new DOMDocument() then loadHTML($html) and do getElementsByTagName('attachfiles') then iterate through the ->length with ->item(i), then do what you want.. replaceChild or whatever.

meder
Thanks Topera for the link and Meder for your solution!.
Ivano
I ve been reading the regular expression modificators and I solved the issue using a couple of them. Using "|\<attachfiles\>(.*)\<\/attachfiles\>|msU" worke perfect!. Thanks again!!
Ivano