预替换以排除<;a href='''&燃气轮机</a>;PHP
我使用preg_replace将文本中的关键字替换为href标记,我的正则表达式工作得非常好,现在我的代码是:预替换以排除<;a href='''&燃气轮机</a>;PHP,php,regex,replace,preg-replace,Php,Regex,Replace,Preg Replace,我使用preg_replace将文本中的关键字替换为href标记,我的正则表达式工作得非常好,现在我的代码是: $newstring2 = preg_replace("/\p{L}*?".preg_quote($match[$i])."\p{L}*/ui", "<a href='".$url."' class='link'>$0</a>", $newstring); 现在多亏了我的正则表达式,我得到了: $text= 'this is sample <a href
$newstring2 = preg_replace("/\p{L}*?".preg_quote($match[$i])."\p{L}*/ui", "<a href='".$url."' class='link'>$0</a>", $newstring);
现在多亏了我的正则表达式,我得到了:
$text= 'this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.'
我需要找到并用href标记替换完整世界,例如:
Key,Keyword,Keyword,keylock,mykey,keys或还有Key,Keyword使用UNICODE支持如何使用负前瞻 解释:捕获所有被称为
文本的关键字
,并将其替换为某个链接,但不捕获后面有
的关键字
$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\'>$1</a>';
$result = preg_replace($re, $subst, $str);
echo $result;
$re='/(text)(?!)/m';
$str='这是关于文本的示例文本。
这是关于什么是什么的样本;
$subst='';
$result=preg_replace($re,$subst,$str);
回声$结果;
输出:
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
但是,如果a
中有
的话,就会出现这种缺陷。e、 g.onclick='write(“”)
解析器确实是最好的方法。HTML和Regex有很多问题。您能告诉我们一些给定输入的预期输出吗?是否存在一个问题?您没有使用DOM解析器?正则表达式不可能进行整个HTML解析,因为它依赖于匹配开头和结尾标记,而这在regexps中是不可能的。应该可以呈现一个HTML文件,该文件将被任何正则表达式错误地匹配。另请参见:,Ok@Cid I添加了我如何使用它,这具有相同的效果,尽管如果属性具有该值,这可能会起作用,但此preg_替换必须排除href并匹配任何完整关键字,没有区别。关键字之间,关键字mykeyword。所以你认为,你发布的内容可以用在我上面给出的正则表达式中吗?我需要这样的东西:$newstring1=preg_replace(“/\p{L}*?/(text)(?!)\p{L}*/ui“,”,$newstring);
$keyword = 'key';
$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\'>$1</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>.
<a[\S\s]+?<\/a>(*SKIP)(*FAIL)|\bTERM\b