正则表达式 - 仅在HTML中匹配标记名称 [英] Regex - Matching Tag Names Only in HTML

查看:117
本文介绍了正则表达式 - 仅在HTML中匹配标记名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用正则表达式来检索html代码片段中的所有html标签名称?我使用PHP来做到这一点,如果它很重要。例如:

How can I use regex to retrieve all html tag names within an html snippet? I'm using PHP to do this if it matters. For example:

<div id="someid">
     <img src="someurl" />
     <br />
     <p>some content</p>
</div>

应该返回:div,img,br,p。

should return: div, img, br, p.

推荐答案

正则表达式可能无法正常工作。如果你100%确定它是格式良好的XHTML,则正则表达式可能是一种可行的方法。如果没有,使用某种PHP库来做到这一点。在C#中,有一种称为HTML Agility Pack的东西, http://htmlagilitypack.codeplex.com ,例如请参阅如何在C#中使用正则表达式解析HTML? 。也许在PHP中有一个等效的工具。

Regexes might not always work. If you're 100% sure that it's well formed XHTML, regexes could be a way to do it though. If not, use some sort of PHP library to do it. In C#, there is something called the HTML Agility Pack, http://htmlagilitypack.codeplex.com, e.g. see How do I parse HTML using regular expressions in C#?. Maybe there is an equivalent tool in PHP.

这篇关于正则表达式 - 仅在HTML中匹配标记名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆