preg_replace 但如果替换文本出现在标题标记内则不匹配
下面的函数将对内容(即 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要对 HTML/XML 使用正则表达式:
Don't use regexes for HTML/XML: