第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP正則表達式以匹配HTML標記<a>之外的關鍵字

PHP正則表達式以匹配HTML標記<a>之外的關鍵字

PHP
有只小跳蛙 2020-02-02 15:15:28
我一直在嘗試做一個正則表達式來匹配和替換HTML一部分上關鍵字的出現(xiàn):我想匹配keyword和<strong>keyword</strong>但是<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>標記中時才可以。任何幫助都感激不盡!
查看完整描述

3 回答

?
炎炎設計

TA貢獻1808條經(jīng)驗 獲得超4個贊

$str = preg_replace('~Moses(?!(?>[^<]*(?:<(?!/?a\b)[^<]*)*)</a>)~i',

                    '<a href="novo-mega-link.php">$0</a>', $str);

否定前瞻中的表達式與下一個結(jié)束</a>標記匹配,但前提是它首先沒有看到開始<a>標記。如果成功,則表示單詞Moses在錨元素內(nèi),因此超前失敗,并且不會發(fā)生匹配。


查看完整回答
反對 回復 2020-02-02
?
森欄

TA貢獻1810條經(jīng)驗 獲得超5個贊

我設法通過以下方式完成了我想做的事情(不使用Regex):


解析字符串的每個字符

刪除所有<a>標簽(將它們復制到臨時數(shù)組并在字符串上保留占位符)

str_replace 新字符串以替換所有關鍵字

通過原始<a>標簽重新填充占位符

如果有人需要,這是我使用的代碼:


$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;

輸出:


鏈接數(shù)組:


Array

(

    [0] => <a href="original-moses1.html">Moses</a>

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


查看完整回答
反對 回復 2020-02-02
?
繁星淼淼

TA貢獻1775條經(jīng)驗 獲得超11個贊

$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 );


查看完整回答
反對 回復 2020-02-02
  • 3 回答
  • 0 關注
  • 616 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號