preg_replace 排除 <a href='''></a> PHP
preg_replace to exclude <a href='''></a> PHP
我使用 preg_replace 将文本中的关键字替换为 href 标签,我的正则表达式工作得很好,现在我的代码是:
$newstring2 = preg_replace("/\p{L}*?".preg_quote($match[$i])."\p{L}*/ui", "<a href='".$url."' class='link'>[=13=]</a>", $newstring);
唯一的问题是,我需要排除 <a href='https://keyword.cz' title="keyword">keyword</a>
中的任何关键字
这是我发现的
所以有人可以帮我将这两个正则表达式合并在一起吗?
示例:
$text = 'this is sample text about something what is text.'
$keyword = 'text'
现在多亏了我的正则表达式,我得到:
$text= 'this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.'
但是如果文本是:
$text= 'this is sample <a href='text.php'>text</a> about something what is <a href='somelink.php'>text</a>.'
这是我得到的示例:
$text= 'this is sample <a href='<a href='somelink.php'>text.php</a>'><a href='somelink.php'>text</a></a> about something what is <a href='somelink.php'><a href='somelink.php'>text</a></a>.'
更新:
为什么我需要这个。
致力于在充满标签的特定博客 post 中用特定 URL 替换所有关键字的功能。
例如,如果
$keyword = 'key';
我需要用 href 标签查找并替换整个世界,例如:
Key, Keyword, keyword, keylock, mykey, keys or also Key, Keyword with UNICODE support
负前瞻怎么样。 Regex
说明:捕获所有名为text
的关键字并用它替换一些link但不要 捕获那些后面有 </a>
的关键字。
$re = '/(text)(?!<\/a>)/m';
$str = 'this is sample text about something what is text.
this is sample <a href=\'somelink.php\'>text</a> about something what is <a href=\'somelink.php\'>text</a>.';
$subst = '<a href=\'somelink.php\'></a>';
$result = preg_replace($re, $subst, $str);
echo $result;
输出:
this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.
this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.
如果必须使用正则表达式,我认为 PCRE 动词是您的最佳选择。排除所有链接,然后搜索带有单词边界的术语。
<a[\S\s]+?<\/a>(*SKIP)(*FAIL)|\bTERM\b
演示:https://regex101.com/r/KlE1kc/1/
不过,如果 a
曾经有过 </a>
,那么这个问题的一个例子就是。例如onclick='write("</a>")'
解析器确实是最好的方法。 HTML 和正则表达式有很多问题。
我使用 preg_replace 将文本中的关键字替换为 href 标签,我的正则表达式工作得很好,现在我的代码是:
$newstring2 = preg_replace("/\p{L}*?".preg_quote($match[$i])."\p{L}*/ui", "<a href='".$url."' class='link'>[=13=]</a>", $newstring);
唯一的问题是,我需要排除 <a href='https://keyword.cz' title="keyword">keyword</a>
这是我发现的
所以有人可以帮我将这两个正则表达式合并在一起吗?
示例:
$text = 'this is sample text about something what is text.'
$keyword = 'text'
现在多亏了我的正则表达式,我得到:
$text= 'this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.'
但是如果文本是:
$text= 'this is sample <a href='text.php'>text</a> about something what is <a href='somelink.php'>text</a>.'
这是我得到的示例:
$text= 'this is sample <a href='<a href='somelink.php'>text.php</a>'><a href='somelink.php'>text</a></a> about something what is <a href='somelink.php'><a href='somelink.php'>text</a></a>.'
更新: 为什么我需要这个。 致力于在充满标签的特定博客 post 中用特定 URL 替换所有关键字的功能。 例如,如果
$keyword = 'key';
我需要用 href 标签查找并替换整个世界,例如: Key, Keyword, keyword, keylock, mykey, keys or also Key, Keyword with UNICODE support
负前瞻怎么样。 Regex
说明:捕获所有名为text
的关键字并用它替换一些link但不要 捕获那些后面有 </a>
的关键字。
$re = '/(text)(?!<\/a>)/m';
$str = 'this is sample text about something what is text.
this is sample <a href=\'somelink.php\'>text</a> about something what is <a href=\'somelink.php\'>text</a>.';
$subst = '<a href=\'somelink.php\'></a>';
$result = preg_replace($re, $subst, $str);
echo $result;
输出:
this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.
this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.
如果必须使用正则表达式,我认为 PCRE 动词是您的最佳选择。排除所有链接,然后搜索带有单词边界的术语。
<a[\S\s]+?<\/a>(*SKIP)(*FAIL)|\bTERM\b
演示:https://regex101.com/r/KlE1kc/1/
不过,如果 a
曾经有过 </a>
,那么这个问题的一个例子就是。例如onclick='write("</a>")'
解析器确实是最好的方法。 HTML 和正则表达式有很多问题。