PHP正则表达式匹配HTML标签之外的关键字< a> [英] PHP Regular expression to match keyword outside HTML tag <a>

查看:198
本文介绍了PHP正则表达式匹配HTML标签之外的关键字< a>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用正则表达式来匹配并替换HTML中一部分关键字的出现:


  1. i想匹配关键字< strong>关键字< / strong>

  2. < a href =someurl.htmltarget =_ blank>关键字< / a> < a href = someur2.html>已链接的关键字< / a> 不应匹配



m只关心在第一行匹配(和替换)关键字



我想要的原因是用< a href =dictionary.php?k = keyword>关键字< / s>替换关键字 ,但只有关键字它不在< a> 标记中。



任何帮助都将非常感谢!

解决方案

我设法做我想做的事>不使用Regex ):




  • pa解析我的字符串中的每个字符

  • 删除所有的< a> 标记(将它们复制到临时数组并保留占位符字符串)
  • str_replace 为了替换所有关键字而添加的新字符串
  • 重新填充占位符是原来的< a> 标记



以下是我使用的代码,以防其他人需要它:

  $ str =<<<< STRA 
摩西假设他的遗ses是玫瑰,
但是< a href =original-moses1.html>摩西< / a>错误地假设;
对于没有人的脚印来说是玫瑰花,
是摩西认为他的遗嘱。
Ganda< / span>< / span>< / span>< a href =original-moses2.htmltarget =_ blank>
STRA;

$ arr1 = str_split($ str);

$ arr_links = array();
$ phrase_holder ='';
$ current_a = 0;
$ goto_arr_links = false;
$ close_a = false;
$ b $ foreach($ arr1 as $ k => $ v)
{
if($ close_a == true)
{
if($ v =='>'){
$ close_a = false;
}
继续;
}

if($ goto_arr_links == true)
{
$ arr_links [$ current_a]。= $ v;
}

if($ v =='<'& $ amp; $ arr1 [$ k + 1] =='a'){/ *< a * /
//继续收集每一个字符直到< / a>
$ arr_links [$ current_a]。= $ v;
$ goto_arr_links = true;
} elseif($ v =='<'&& $ arr1 [$ k + 1] =='/'&& $ arr1 [$ k + 2] =='a'& amp ;& $ arr1 [$ k + 3] =='>'){/ *< / a> * /
$ arr_links [$ current_a]。=/ a>;

$ goto_arr_links = false;
$ close_a = true;
$ phrase_holder。={%$ current_a%}; / *在短语* /
$ current_a ++中放置一个参数持有者;
}
elseif($ goto_arr_links == false){
$ phrase_holder。= $ v;
}
}

echoLinks Array:\\\
;
print_r($ arr_links);
echo\\\
\\\
\\\
Phrase Holder:\\\
;
echo $ phrase_holder;
echo\\\
\\\
\\\
(pre)Final Phrase(with my keyword replaced):\\\
;
$ final_phrase = str_replace(Moses,< a href = \novo-mega-link.php \>摩西< / a>,$ phrase_holder);
echo $ final_phrase;
echo\\\
\\\
\\\
Final Phrase:\\\
;
foreach($ arr_links as $ k => $ v)
{
$ final_phrase = str_replace({%$ k%},$ v,$ final_phrase);
}
echo $ final_phrase;

输出:

链接数组:

 数组

[0] =>< a href =original-moses1。 Moses< / a>
[1] =>< a href =original-moses2.htmltarget =_ blank> Moses< / a>

短语持有人:

 摩西假设他的玫瑰是玫瑰,
但{%0%}错误地假设;
对于没有人的脚印来说是玫瑰花,
是摩西认为他的遗嘱。
Ganda< span class =cenas> {%1%}< / span> ;!

(pre)最终短语(用我的关键字替换):

 < a href =novo-mega-link.php>摩西< / a>假设他的遗are是玫瑰,
但{%0%}错误地假设;
for nobody's toeses are bloks,
as< a href =novo-mega-link.php>摩西< / a>假设他的遗to是。
Ganda< span class =cenas> {%1%}< / span> ;!

最终短语

 < a href =novo-mega-link.php>摩西< / a>假设他的遗愿是玫瑰,
但是< a href =original-moses1.html>摩西< / a>错误地假设;
for nobody's toeses are bloks,
as< a href =novo-mega-link.php>摩西< / a>假设他的遗to是。
Ganda< / span>< / span>< / span>< a href =original-moses2.htmltarget =_ blank>


I've been trying to do a regex to match and replace the occurrences of a keyword on a portion of HTML:

  1. i want to match keyword and <strong>keyword</strong>
  2. but <a href="someurl.html" target="_blank">keyword</a> and <a href="someur2.html">already linked keyword </a> should NOT be matched

I'm only interested in matching (and replacing) the keyword on the 1st line.

The reason I want this is to replace keyword with <a href="dictionary.php?k=keyword">keyword</s>, but ONLY if keyword it's not already inside an <a> tag.

Any help will be much appreciated!

解决方案

I managed to do what I wanted (without using Regex) by:

  • parsing each character of my string
  • removing all <a> tags (copying them to a temporary array and keeping a placeholder on the string)
  • str_replace the new string in order to replace all the keywords
  • repopulating the placeholders by it's original <a> tags

Here's the code I used, in case someone else needs it:

$str = <<<STRA
Moses 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)
{
    if ($close_a == true)
    {
        if ($v == '>') {
            $close_a = false;
        } 
        continue;
    }

    if ($goto_arr_links == true)
    {
        $arr_links[$current_a] .= $v;
    }

    if ($v == '<' && $arr1[$k+1] == 'a') { /* <a */
        // keep collecting every char until </a>
        $arr_links[$current_a] .= $v;
        $goto_arr_links = true;
    } elseif ($v == '<' && $arr1[$k+1] == '/' && $arr1[$k+2] == 'a' && $arr1[$k+3] == '>' ) { /* </a> */
        $arr_links[$current_a] .= "/a>";

        $goto_arr_links = false;
        $close_a = true;
        $phrase_holder .= "{%$current_a%}"; /* put a parameter holder on the phrase */
        $current_a++;
    }    
    elseif ($goto_arr_links == false) {
        $phrase_holder .= $v;
    }
}

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)
{
    $final_phrase = str_replace("{%$k%}", $v, $final_phrase);
}
echo $final_phrase;

The output:

Links Array:

Array
(
    [0] => <a href="original-moses1.html">Moses</a>
    [1] => <a href="original-moses2.html" target="_blank">Moses</a>
)

Phrase Holder:

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>!

(pre) Final Phrase (with my keyword replaced):

<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>!

Final Phrase:

<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>!

这篇关于PHP正则表达式匹配HTML标签之外的关键字&lt; a&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆