5

データベース テーブルに格納されている html コンテンツがあります。そのhtmlコンテンツで、「SOME WORDS」をリンクタグに置き換えたいと思います。しかし、「SOME WORDS」がすでにリンクタグ内にある場合は、それらを省略すべきです..


: コンテンツ

<p>Lorem ipsum dolor SOME WORDS, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>

出力は

<p>Lorem ipsum dolor <a href="http://someurl">SOME WORDS</a>, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>

ご覧のとおり、置換時に既存のリンク テキストを除外する必要があります。

正しい軌道に乗るためのいくつかのガイダンスは非常に高く評価されています.

4

4 に答える 4

3

これはDOMDocument、正規表現の代わりに使用してそれを解決する方法です。

$contents = <<<EOS
<p>Lorem ipsum dolor SOME WORDS, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>
EOS;

$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc->loadHTML($contents);
libxml_clear_errors();

$xp = new DOMXPath($doc);

// find all text nodes
foreach ($xp->query('//text()') as $node) {
        // make sure it's not inside an anchor
        if ($node->parentNode->nodeName !== 'a') {
                $node->nodeValue = str_replace(
                    'SOME WORDS', 
                    'SOME OTHER WORDS', 
                    $node->nodeValue
                );
        }
}
// DOMDocument creates a full document and puts your fragment inside a body tag
// So we enumerate the children and save their HTML representation
$body = $doc->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $node) {
        echo $doc->saveHTML($node);
}
于 2012-12-15T09:09:10.783 に答える
1

3行のスペースがある場合、これは安全な方法です。

$text=preg_replace('~<a(.*)(SOME WORDS)(.*)</a>~','<a$1PLACEHOLDER$3</a>',$text);
$text=preg_replace('~SOME WORDS~','REPLACEMENT WORDS',$text);
$text=preg_replace('~PLACEHOLDER~','SOME WORDS',$text);

PLACEHOLDERのテキスト/タグなどを使用するため、リンクの内容を置き換えることはありません(リンクの内容がある場合)。

于 2012-12-15T07:05:13.860 に答える
1

単純な正規表現は、それが正確なフレーズであり、リンク内に他の記号や単語がない場合にのみ機能します。のすべての出現を反復処理してSOME WORDS、出現前に開始リンクタグと終了リンクタグがあった回数を計算することにより、それらがリンク内にあるかどうかを確認できます。このコードを試してください:

$str = '<p>Lorem ipsum dolor SOME WORDS, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>';
echo 'Before:' . $str;
$str_lc = strtolower($str);
$phrase = 'SOME WORDS';
$link = '<a href="http://someurl">SOME WORDS</a>';
$offset = 0;
while($position = strpos($str, $phrase, $offset))
{
    if (substr_count($str_lc, "<a", 0, $position) <= substr_count($str_lc, "</a>", 0, $position)) {
        $str = substr_replace($str, $link, $position, strlen($phrase));
        $str_lc = strtolower($str);
        $offset = $position + strlen($link) - strlen($phrase);
    } else {
        $offset = $position + 1;
    }
}
echo 'After:' . $str;
于 2012-12-15T06:47:02.473 に答える
0

これでうまくいくはずです。

一部の単語がタグで囲まれているかどうかを正規表現で確認してください

preg_replace('/[^>]SOME WORDS[^<]/','<a href="http://someurl">SOME WORDS</a>',$str);
于 2012-12-15T06:40:09.047 に答える