PHP:读取和查找 HTML 内容的内置方法
如果我理解正确的话,在 HTML 中查找内容时,您应该远离正则表达式。内置于标准 PHP 中的一个好的替代方案是什么?
在我现在的特殊情况下,我想找到所有图像标签及其 src、alt、height 和 width 属性。后来我还想找到某些元标签。不管怎样,你会如何使用 PHP 来做到这一点?
我的虚拟主机上的 PHP 版本当前为 5.2.x。
If I have understood correctly, you should stay away from regular expressions when it comes to finding stuff in HTML. What is a good alternative to that, which is built in to standard PHP?
In my particular case right now I want to find all the image tags with their src, alt, height and width attributes. Later I'd also want to find certain meta tags. Either way, how would you do this with PHP?
The PHP version on my webhost is currently 5.2.x.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您始终可以使用一些 PHP DOM 方法
You can always use some PHP DOM methods
DOMXPath
对象允许您在 PHP 中针对 XML 运行 XPath 查询。 XPath 允许您从 XML 文档中提取特定标签。它是语言中立的(如正则表达式),并且几乎所有编程语言都支持它。The
DOMXPath
object allows you to run XPath queries against XML in PHP. XPath allows you to extract specific tags from XML documents. It is language neutral (like regular expressions) and practically every programming language supports it.