PHP正则表达式以匹配HTML标记<a>之外的关键字

我一直在尝试做一个正则表达式来匹配和替换HTML一部分上关键字的出现:

  1. 我想匹配keyword<strong>keyword</strong>

  2. 但是<a href="someurl.html" target="_blank">keyword</a>并且<a href="someur2.html">already linked keyword </a>不应该匹配

我只对keyword第一行的匹配(和替换)感兴趣。

我想要此操作的原因是将其替换keyword<a href="dictionary.php?k=keyword">keyword</s>,但仅当keyword它尚未位于<a>标记中时才可以。

任何帮助都感激不尽!


有只小跳蛙
浏览 588回答 3
3回答

炎炎设计

$str = preg_replace('~Moses(?!(?>[^<]*(?:<(?!/?a\b)[^<]*)*)</a>)~i',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="novo-mega-link.php">$0</a>', $str);否定前瞻中的表达式与下一个结束</a>标记匹配,但前提是它首先没有看到开始<a>标记。如果成功,则表示单词Moses在锚元素内,因此超前失败,并且不会发生匹配。

森栏

我设法通过以下方式完成了我想做的事情(不使用Regex):解析字符串的每个字符删除所有<a>标签(将它们复制到临时数组并在字符串上保留占位符)str_replace 新字符串以替换所有关键字通过原始<a>标签重新填充占位符如果有人需要,这是我使用的代码:$str = <<<STRAMoses supposes his toeses are roses,but <a href="original-moses1.html">Moses</a> supposes erroneously;for nobody's toeses are posies of roses,as Moses supposes his toeses to be.Ganda <span class="cenas"><a href="original-moses2.html" target="_blank">Moses</a></span>!STRA;$arr1 = str_split($str);$arr_links = array();$phrase_holder = '';$current_a = 0;$goto_arr_links = false;$close_a = false;foreach($arr1 as $k => $v){&nbsp; &nbsp; if ($close_a == true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($v == '>') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $close_a = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; }&nbsp; &nbsp; if ($goto_arr_links == true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $arr_links[$current_a] .= $v;&nbsp; &nbsp; }&nbsp; &nbsp; if ($v == '<' && $arr1[$k+1] == 'a') { /* <a */&nbsp; &nbsp; &nbsp; &nbsp; // keep collecting every char until </a>&nbsp; &nbsp; &nbsp; &nbsp; $arr_links[$current_a] .= $v;&nbsp; &nbsp; &nbsp; &nbsp; $goto_arr_links = true;&nbsp; &nbsp; } elseif ($v == '<' && $arr1[$k+1] == '/' && $arr1[$k+2] == 'a' && $arr1[$k+3] == '>' ) { /* </a> */&nbsp; &nbsp; &nbsp; &nbsp; $arr_links[$current_a] .= "/a>";&nbsp; &nbsp; &nbsp; &nbsp; $goto_arr_links = false;&nbsp; &nbsp; &nbsp; &nbsp; $close_a = true;&nbsp; &nbsp; &nbsp; &nbsp; $phrase_holder .= "{%$current_a%}"; /* put a parameter holder on the phrase */&nbsp; &nbsp; &nbsp; &nbsp; $current_a++;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; elseif ($goto_arr_links == false) {&nbsp; &nbsp; &nbsp; &nbsp; $phrase_holder .= $v;&nbsp; &nbsp; }}echo "Links Array:\n";print_r($arr_links);echo "\n\n\nPhrase Holder:\n";echo $phrase_holder;echo "\n\n\n(pre) Final Phrase (with my keyword replaced):\n";$final_phrase = str_replace("Moses", "<a href=\"novo-mega-link.php\">Moses</a>", $phrase_holder);echo $final_phrase;echo "\n\n\nFinal Phrase:\n";foreach($arr_links as $k => $v){&nbsp; &nbsp; $final_phrase = str_replace("{%$k%}", $v, $final_phrase);}echo $final_phrase;输出:链接数组:Array(&nbsp; &nbsp; [0] => <a href="original-moses1.html">Moses</a>&nbsp; &nbsp; [1] => <a href="original-moses2.html" target="_blank">Moses</a>)词组:Moses supposes his toeses are roses,but {%0%} supposes erroneously;for nobody's toeses are posies of roses,as Moses supposes his toeses to be.Ganda <span class="cenas">{%1%}</span>!(上)最终词组(替换为我的关键字):<a href="novo-mega-link.php">Moses</a> supposes his toeses are roses,but {%0%} supposes erroneously;for nobody's toeses are posies of roses,as <a href="novo-mega-link.php">Moses</a> supposes his toeses to be.Ganda <span class="cenas">{%1%}</span>!最终词组:<a href="novo-mega-link.php">Moses</a> supposes his toeses are roses,but <a href="original-moses1.html">Moses</a> supposes erroneously;for nobody's toeses are posies of roses,as <a href="novo-mega-link.php">Moses</a> supposes his toeses to be.Ganda <span class="cenas"><a href="original-moses2.html" target="_blank">Moses</a></span>!

繁星淼淼

$lines = explode( "\n", $content );$lines[0] = stri_replace( "keyword", "replacement", $lines[0] );$content = implode( "\n", $lines );或者如果您明确想使用正则表达式$lines = explode( "\n", $content );$lines[0] = preg_replace( "/keyword/i", "replacement", $lines[0] );$content = implode( "\n", $lines );
打开App,查看更多内容
随时随地看视频慕课网APP