开发者

Select a tag from multiline html code using preg_match_all

开发者 https://www.devze.com 2023-01-17 04:39 出处:网络
I want to use the php function preg_match_all to find a part of the html code to replace it by another one.

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 开发者_StackOverflowthe code that I am searching for.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号