Php 在内容片段中第一次出现的关键字周围加上粗体/强标记?

Php 在内容片段中第一次出现的关键字周围加上粗体/强标记?,php,regex,preg-replace,domdocument,Php,Regex,Preg Replace,Domdocument,我正在寻找一种最简单的方法,在预定义关键字短语的第一次出现时,如果该短语没有出现在标题标记或html属性值中,则可以将粗体标记包装起来。找到第一个匹配项后,退出例程 例如,如果关键字是“blue widgets”,而内容是: blue widgets and accessories for blue widgets can be found here 然后,在例程过滤内容后,它将返回: <b>blue widgets</b> and accessories for bl

我正在寻找一种最简单的方法,在预定义关键字短语的第一次出现时,如果该短语没有出现在标题标记或html属性值中,则可以将粗体标记包装起来。找到第一个匹配项后,退出例程

例如,如果关键字是“blue widgets”,而内容是:

blue widgets and accessories for blue widgets can be found here
然后,在例程过滤内容后,它将返回:

<b>blue widgets</b> and accessories for blue widgets can be found here
蓝色小部件和蓝色小部件的附件可以在这里找到
但是,如果单词“bluewidgets”第一次出现在属性或标题标记中,它将跳过这些标记并转到下一个。比如说,

<img src="foo.png" title="A site about blue widgets" alt="blue-widget" />
<h2>This is a site about blue widgets</h2>
<p>We've got lots of blue widgets and blue widget accessories...

这是一个关于蓝色小部件的网站
我们有很多蓝色小部件和蓝色小部件配件。。。
在上面的内容中,只有“我们有很多蓝色小部件和蓝色小部件附件”这句话中的关键字出现。。。我会大胆的


有人能给我举个例子说明如何做到这一点吗?

如果您仍在考虑使用正则表达式,请查看以下内容:

$source = <<<EOS
<img src="foo.png" title="A site about blue widgets" alt="blue-widget" />
<h2>This is a site about blue widgets</h2>
<p>We've got lots of blue widgets and blue widget accessories...';
EOS;

$term = 'blue widgets';

// convert search term to valid regex
$term0 = preg_replace(array('~\A\b~', '~\b\z~', '~\s+~'), 
                      array('\b', '\b', '\s+'),
                      preg_quote(trim($term), '~'));

$regex = <<<EOR
~\A   # anchoring at string start ensures only one match can occur
(?>
   <(h[1-6])[^>]*>.*?</\\1>   # a complete h<n> element
 | </?\w+[^>]*+>              # any other tag
 | (?:(?!<|{$term0}).)*+      # anything else, but stop before '<' or the search term
)*+
\K    # pretend the match really started here; only the next part gets replaced
{$term0}
~isx
EOR;

echo preg_replace($regex, "<strong>$0</strong>", $source);

$source=如果您仍在考虑使用正则表达式,请查看以下内容:

$source = <<<EOS
<img src="foo.png" title="A site about blue widgets" alt="blue-widget" />
<h2>This is a site about blue widgets</h2>
<p>We've got lots of blue widgets and blue widget accessories...';
EOS;

$term = 'blue widgets';

// convert search term to valid regex
$term0 = preg_replace(array('~\A\b~', '~\b\z~', '~\s+~'), 
                      array('\b', '\b', '\s+'),
                      preg_quote(trim($term), '~'));

$regex = <<<EOR
~\A   # anchoring at string start ensures only one match can occur
(?>
   <(h[1-6])[^>]*>.*?</\\1>   # a complete h<n> element
 | </?\w+[^>]*+>              # any other tag
 | (?:(?!<|{$term0}).)*+      # anything else, but stop before '<' or the search term
)*+
\K    # pretend the match really started here; only the next part gets replaced
{$term0}
~isx
EOR;

echo preg_replace($regex, "<strong>$0</strong>", $source);

$source=使用
而不是
;)在HTML上这样做似乎是错误的。。。您无法控制生成HTML的系统?可能重复-方法相同。查找所有文本节点。迭代它们。找到关键字并将其包装到所需的标记中,而不是URL。@Casper,当然可以。我只是为了简单起见才使用b,但你是对的,strong是语义上更正确的方法。@Gordon,在这个参考点上有一些很好的例子。谢谢使用
而不是
;)在HTML上这样做似乎是错误的。。。您无法控制生成HTML的系统?可能重复-方法相同。查找所有文本节点。迭代它们。找到关键字并将其包装到所需的标记中,而不是URL。@Casper,当然可以。我只是为了简单起见才使用b,但你是对的,strong是语义上更正确的方法。@Gordon,在这个参考点上有一些很好的例子。谢谢