我使用 preg_replace 用 href 标签替换文本中的关键字,我的正则表达式运行得很好,现在我的代码是:
$newstring2 = preg_replace("/\p{L}*?".preg_quote($match[$i])."\p{L}*/ui", "<a href='".$url."' class='link'>$0</a>", $newstring);
唯一的问题是,我需要排除里面的任何关键字<a href='https://keyword.cz' title="keyword">keyword</a>
这就是我发现的https://stackoverflow.com/a/22821650/4928816
那么有人可以帮助我将这两个正则表达式合并在一起吗?
Example:
$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>.'
更新:
为什么我需要这个。
正在开发将所有关键字替换为充满标签的特定博客文章中的特定 URL 的功能。
例如如果
$keyword = 'key';
我需要查找并用 href 标签替换整个世界,例如:Key、Keyword、keyword、keylock、mykey、keys 或 KeY、Keyword具有 UNICODE 支持