如何为 preg_match_all 创建模式
我尝试用谷歌搜索这个,但找不到任何明确的内容。首先,我希望有人可以帮助我编写一个模式来获取这些标签之间的信息:
<vboxview leftinset="10" rightinset="0" stretchiness="1"> // CONTENT INSIDE HERE </vboxview>
其次,您能否详细解释每个部分的模式以及它的作用以及如何指定获取代码的特定部分。
I tried googling this but I couldnt find anything clear about it. first I was hoping someone could help me write a pattern to get the info between these tags :
<vboxview leftinset="10" rightinset="0" stretchiness="1"> // CONTENT INSIDE HERE </vboxview>
and second, could you also please explain the pattern in details for each section and what it does and how you specify to get a certain part of the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅我对这个问题的评论,了解我对基于 SGML 的语言和正则表达式的咆哮......
现在我的答案。
如果您知道相关标签内不会有任何其他 HTML/XML 元素,那么这将工作得很好:
分解后,此表达式表示:
您将需要转义和
>
字符源作为\>
甚至更好作为 HTML/XML 实体如果内部有嵌套结构,那么您要么 开始遇到正则表达式问题,或者您将已经决定使用另一种不涉及正则表达式的方法 - 任何一种方法都足够了!
See my comment on the question for my rant on SGML-based languages and regex...
Now to my answer.
If you know there will not be any other HTML/XML elements inside the tag in question, then this will work quite well:
Broken down, this expression says:
You will need to escape and
>
characters inside the source as\>
or even better as HTML/XML entitiesIf there are going to be nested constructs inside, then you are either going to start running into problems with regex, or you will have already decided to use another method that does not involve regex - either way is sufficient!
正如评论中所提到的,尝试使用正则表达式从 HTML 中提取内容通常不是一个好主意。如果您想切换到更可靠的方法,这里有一个快速示例,说明如何使用 DOMDocument API。
更好的是,如果保证您的输入中只有一个
vboxview
(同时假设您可以控制 HTML),您可以向vboxview< 添加一个
id
属性/code> 并将代码缩减为更短、更通用的函数。As it has been mentioned in the comments it is usually not a good idea to try to extract things from HTML with regular expressions. If you ever want to switch to a more bulletproof method here's a quick example of how you could easily extract the information using the DOMDocument API.
Better yet if there is guaranteed to be only one
vboxview
in your input (also assuming you have control of the HTML) you could add anid
attribute tovboxview
and cut the code down to a shorter and more generalized function.