正则表达式 php 在 html 标记中查找一个字符


Regex php find a character within html tag

我陷入了一个似乎无法解决的顽固问题。

我尝试仅在 html 标签内(而不是两者之间)查找特定字符时。

为了测试这个,我有 2 个测试字符串:

  1. 没有 HTML 的字符串。这是句子 2。
  2. 带有一些 HTML 的字符串。 this is <a href="www.somesite.com">sentence</a>

我想在 html 标签中找到<>句点字符,因此匹配应该是 www.somesite.com 内的 2 个句点,我无法正确获得匹配。有人可以看看我的正则表达式,看看我错过了什么吗?

(<[^>]*>?('.))>?

试试这个:

$re = "/>[^<]*<(*SKIP)(*F)|searchText/mi";   //before | part avoid tag inner text and after | part search only tag inside text.
$str = "<div><a href='"www.searchText.com'">This is <a href='"www.searchText.com'">sentence</a> tI want to test.</a></div>";
preg_match_all($re, $str, $matches);

演示

给定字符串 " This is <a href="www.somesite.com">sentence</a> I want to test." 正则表达式:

'.(?='w)

将匹配 URL 中的句点,但不匹配句子末尾的句点。请注意,正则表达式不是特定于 URL 的,它只是使用正面前瞻查找一个句点,后跟一个单词字符。

话虽如此,你真的应该用PHPDomDocument这样的东西来解析HTML