preg_replace 但如果替换文本出现在标题标记内则不匹配

发布于 2024-09-30 21:32:25 字数 982 浏览 3 评论 0原文

下面的函数将对内容(即 html 标记)进行替换,将粗体和 em 标记包围在它找到的关键字的前两次出现处。

我需要考虑的一种情况是,如果关键字已经在 h1 标记内,我不希望发生回调。

示例:

这是标题标签内的关键字

替换后

这是关键字标题标签内

我如何更改替换,以便它跳过出现在标题标签 (h1-h6) 内的关键字并转到下一个匹配项?

function doReplace($matches)
{
    static $count = 0;
    switch($count++) {
        case 0: return ' <b>'.trim($matches[1]).'</b>';
        case 1: return ' <em>'.trim($matches[1]).'</em>';
        default: return $matches[1];
            }
    }

function save_content($content){
    $mykeyword = "test";
    if ((strpos($content,"<b>".$mykeyword) > -1 || 
    strpos($content,"<strong>".$mykeyword) > -1) && 
    strpos($content,"<em>".$mykeyword) > -1 ) 
    {
        return $content;
    }
    else
    {
        $theContent = preg_replace_callback("/\b(?<!>)($mykeyword)\b/i","doReplace", $content);
        return $theContent;
    }
}

The functions below, will do a replace on the content (which is html markup) wrapping bold and em tags around the first two occurrences of the keyword that it finds.

The one case I need to account for though, is if the keyword is already inside of an h1 tag I don't want the callback to occur.

Example:

<h1>this is the keyword inside of a heading tag</h1>

After replacement

<h1>this is the <b>keyword</b> inside of a heading tag</h1>

How might I alter the replacement so that it skips over keywords that appear inside a heading tag (h1-h6) and moves on to the next match?

function doReplace($matches)
{
    static $count = 0;
    switch($count++) {
        case 0: return ' <b>'.trim($matches[1]).'</b>';
        case 1: return ' <em>'.trim($matches[1]).'</em>';
        default: return $matches[1];
            }
    }

function save_content($content){
    $mykeyword = "test";
    if ((strpos($content,"<b>".$mykeyword) > -1 || 
    strpos($content,"<strong>".$mykeyword) > -1) && 
    strpos($content,"<em>".$mykeyword) > -1 ) 
    {
        return $content;
    }
    else
    {
        $theContent = preg_replace_callback("/\b(?<!>)($mykeyword)\b/i","doReplace", $content);
        return $theContent;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛滥成性 2024-10-07 21:32:25

不要对 HTML/XML 使用正则表达式:

$d = new DOMDocument();
$d->loadHTML($your_html);
$x = new DOMXpath($d);
foreach($x->query("//text()[
   contains(.,'keyword')
   and not(ancestor::h1) 
   and not(ancestor::h2) 
   and not(ancestor::h3) 
   and not(ancestor::h4) 
   and not(ancestor::h5) 
   and not(ancestor::h6)]") as $node){
    //do with the node as you like
}       

Don't use regexes for HTML/XML:

$d = new DOMDocument();
$d->loadHTML($your_html);
$x = new DOMXpath($d);
foreach($x->query("//text()[
   contains(.,'keyword')
   and not(ancestor::h1) 
   and not(ancestor::h2) 
   and not(ancestor::h3) 
   and not(ancestor::h4) 
   and not(ancestor::h5) 
   and not(ancestor::h6)]") as $node){
    //do with the node as you like
}       
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文