使用 preg_match_all 从多行 html 代码中选择一个标签
我想使用php函数preg_match_all来查找html代码的一部分以将其替换为另一部分。
这就是我需要找到的:
<attachfiles>
tag{link} attr{rel="stylesheet" type="text/css" media="screen"}
sources{
file1.css,
file2.css
}
</attachfiles>
我创建了一个正则表达式来找到它,但前提是该代码在整个 html 中出现一次。
我的正则表达式是:
"|\<attachfiles\>(.*)\<\/attachfiles\>|s"
当我有要查找重复两次或多次的代码时,就会出现问题。由于正则表达式使用 |s 运算符(多行),因此当我多次使用该代码时,它会返回从第一个到最后一个的所有 html 代码
例如:
<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>
在这种情况下,我的正则表达式将返回所有代码,从第一个开始,
<attachfiles> to the last </attachfiles>
包括
... html code ...
... html code ...
我正在搜索的代码之间的内容。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 DOM 并创建一个
new DOMDocument()
然后loadHTML($html)
并执行getElementsByTagName('attachfiles')
然后迭代 < code>->length 与->item(i)
,然后做你想做的事..replaceChild
或其他什么。Use the DOM and create a
new DOMDocument()
thenloadHTML($html)
and dogetElementsByTagName('attachfiles')
then iterate through the->length
with->item(i)
, then do what you want..replaceChild
or whatever.