正则表达式:匹配 HTML 文件中的所有 alt 属性?

发布于 2024-12-13 20:42:11 字数 589 浏览 7 评论 0原文

我一直在研究问题并更好地了解我的问题,但仍然没有找到答案。

我在 PHP 中的正则表达式方面遇到问题。我正在尝试获取 HTML 文件的“alt”属性中的所有文本。我考虑了所有可能的标签名称(img、输入和区域)以及各种可能发生的情况,例如字符之间的空格和换行符(例如 Hello)。还必须注意,匹配字符串可以用单引号或双引号括起来,并在内部包含其他(不同的)引号,例如: Alan's picture 或者, 文本中单词“hello”的示例

这对我来说变得很困难(我是正则表达式的初学者),所以我只会向您展示我所得到的。请注意,我试图在字符类中使用反向引用,我发现这是错误的做法(或者我认为是这样)。

'/<\s*(?:img|输入|区域)\s[^>]*alt\s*=\s*("|\')([^\1>]*) \1[^>]*>/siU'

我也在 StackOverflow 中看到,有些人推荐使用 HTML 解析器来做这样的事情,但我担心这种做法会消耗多少资源。你认为这是有更好的主意吗?谢谢!

I've been looking through the questions and got a better idea of my problem, but still, didn't find an answer.

I have a problem with regular expressions in PHP. I'm trying to get all the text in "alt" attributes of an HTML file. I'm taking into account all the possible tag names (img, input and area) and all kind of eventualities, like spaces and line breaks inbetween the characters (like <img alt = "Hello">). It must also be aware that the match string can be enclosed by single or double quotes and contain other (different) quote marks inside, for example: <img alt="Alan's picture"> or, <img alt='Example for the word "hello" in the text'>.

This is becoming difficult to me (I'm a beginner with regular expressions) so I'll just show you what I got. Note that I'm trying to use a backrefernce inside a character class, which I found to be a wrong practice (or so I think).

'/<\s*(?:img|input|area)\s[^>]*alt\s*=\s*("|\')([^\1>]*)\1[^>]*>/siU'

I've also seen in StackOverflow, some people recommending HTML parsers for stuff like this, but I'm worried about how much resources this practice may consume. Would you think this is a better idea? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我还不会笑 2024-12-20 20:42:11

使用解析器绝对是最佳选择。

正则表达式非常不适合此类任务,并且 即使 Jon Skeet 也无法使用正则表达式解析 HTML

Using a parser is definitely the way to go.

Regex are highly inappropriate for this type of tasks, and even Jon Skeet cannot parse HTML using regular expressions

满天都是小星星 2024-12-20 20:42:11

你绝对应该使用解析器。造成这种情况的原因有几个:

  • HTML 解析器库可以解释正则表达式会错过的损坏(或其他格式错误)的 HTML;例如,某些网页将无法转义 alt 属性中嵌入的引号,例如 alt='why can't I do this'
  • 解析器将能够自动处理转义字符;例如,alt="why the long space"
  • 此外,HTML 解析器很可能会提供速度和 API 优势,

您也许可以查看StackOverflow 问题 强大、成熟的 HTML 解析器对于 PHP 有关哪些解析器值得使用的一些建议。

Absolutely you should use a parser. There are several reasons for this:

  • An HTML parser library can account for broken (or otherwise malformed) HTML that a regular expression will miss; for instance, some webpages will fail to escape quotes embedded in the alt attribute, such as alt='why can't I do this'
  • Parsers will be able to handle escaped characters automatically; for instance, alt="why the long space"
  • Additionally, it's probable that an HTML parser will offer speed and API advantages

You can perhaps check out the StackOverflow question Robust, Mature HTML Parser for PHP for some suggestions about what parsers would be worthwhile to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文