Php Preg Replace-替换第二次出现的匹配
我对php比较陌生,希望有人能帮我替换正则表达式,或者是匹配替换,我不太确定 我想自动加粗(匹配的第二次出现),然后使匹配的第四次出现斜体,然后使匹配的第七次出现下划线 这基本上是为了内容的SEO目的 我已经用:做了一些替换,并且我们认为这应该起作用吗Php Preg Replace-替换第二次出现的匹配,php,preg-replace,preg-match,Php,Preg Replace,Preg Match,我对php比较陌生,希望有人能帮我替换正则表达式,或者是匹配替换,我不太确定 我想自动加粗(匹配的第二次出现),然后使匹配的第四次出现斜体,然后使匹配的第七次出现下划线 这基本上是为了内容的SEO目的 我已经用:做了一些替换,并且我们认为这应该起作用吗 preg_replace( pattern, replacement, subject [, limit ]) 我已经知道我想用的词了 'pattern' is also a word that is already defined like
preg_replace( pattern, replacement, subject [, limit ])
我已经知道我想用的词了
'pattern' is also a word that is already defined like [word].
`replacement` 'This is a variable I am getting from a mysql db.
'subject' - The subject is text from a db.
假设我有这个内容:这或多或少解释了我想做什么
这是我要替换的文本的一个示例。在本文中,我想让单词第二次出现,例如我将如何做到这一点,任何想法都将不胜感激。正则表达式本身无法计数,preg_uu函数提供的帮助也很少。你需要一个变通办法。如果要实际搜索一个单词,可能需要使用字符串函数。否则,请尝试:
// just counting
if (7 >= preg_match_all($pattern, $subject, $matches)) {
$cb_num = 0;
$subject = preg_replace_callback($pattern, "cb_ibu", $subject);
}
function cb_ibu($match) {
global $cb_num;
$match = $match[0];
switch (++$cb_num) {
case 2: return "<b>$match</b>";
case 4: return "<i>$match</i>";
case 7: return "<u>$match</u>";
default: return $match;
}
}
//只是计数而已
如果(7>=preg\u match\u all($pattern,$subject,$matches)){
$cb_num=0;
$subject=preg_replace_回调($pattern,“cb_ibu”,$subject);
}
功能cb_ibu($match){
全球$cb_num;
$match=$match[0];
开关(++$cb_num){
案例2:返回“$match”;
案例4:返回“$match”;
案例7:返回“$match”;
默认值:返回$match;
}
}
诀窍是有一个回调来进行会计处理。在这里,添加任何规则都非常容易。正则表达式本身无法计数,preg_uu函数提供的帮助也很少。你需要一个变通办法。如果要实际搜索一个单词,可能需要使用字符串函数。否则,请尝试:
// just counting
if (7 >= preg_match_all($pattern, $subject, $matches)) {
$cb_num = 0;
$subject = preg_replace_callback($pattern, "cb_ibu", $subject);
}
function cb_ibu($match) {
global $cb_num;
$match = $match[0];
switch (++$cb_num) {
case 2: return "<b>$match</b>";
case 4: return "<i>$match</i>";
case 7: return "<u>$match</u>";
default: return $match;
}
}
//只是计数而已
如果(7>=preg\u match\u all($pattern,$subject,$matches)){
$cb_num=0;
$subject=preg_replace_回调($pattern,“cb_ibu”,$subject);
}
功能cb_ibu($match){
全球$cb_num;
$match=$match[0];
开关(++$cb_num){
案例2:返回“$match”;
案例4:返回“$match”;
案例7:返回“$match”;
默认值:返回$match;
}
}
诀窍是有一个回调来进行会计处理。在这里,添加任何规则都非常容易。这是一个有趣的问题。我的实施将是:
function replace_exact($word, $tag, $string, $limit) {
$tag1 = '<'.$tag.'>';
$tag2 = '</'.$tag.'>';
$string = str_replace($word, $tag1.$word.$tag2, $string, 1);
if ($limit==1) return $string;
return str_replace($tag1.$word.$tag2,$word,$string,$limit-1);
}
我不知道这会有多快,但它会比preg_replace快。这是一个有趣的问题。我的实施将是:
function replace_exact($word, $tag, $string, $limit) {
$tag1 = '<'.$tag.'>';
$tag2 = '</'.$tag.'>';
$string = str_replace($word, $tag1.$word.$tag2, $string, 1);
if ($limit==1) return $string;
return str_replace($tag1.$word.$tag2,$word,$string,$limit-1);
}
我不知道这会有多快,但它会比preg_replace快。您可以使用在匹配处拆分文本,应用修改,然后将所有内容重新组合在一起:
$parts = preg_split('/(example)/', $str, 7, PREG_SPLIT_DELIM_CAPTURE);
if (isset($parts[3])) $parts[3] = '<b>'.$parts[3].'</b>';
if (isset($parts[7])) $parts[7] = '<i>'.$parts[7].'</i>';
if (isset($parts[13])) $parts[13] = '<u>'.$parts[13].'</u>';
$str = implode('', $parts);
$parts=preg_split(“/(示例)/”,$str,7,preg_split_DELIM_CAPTURE);
如果(isset($parts[3]))$parts[3]='.$parts[3].';
如果(isset($parts[7]))$parts[7]='.$parts[7].';
如果(isset($parts[13]))$parts[13]='.$parts[13].';
$str=内爆(“”,$parts);
第i个匹配的索引公式是index=i·2-1。您可以使用该公式在匹配处拆分文本,应用修改,然后将所有内容重新组合在一起:
$parts = preg_split('/(example)/', $str, 7, PREG_SPLIT_DELIM_CAPTURE);
if (isset($parts[3])) $parts[3] = '<b>'.$parts[3].'</b>';
if (isset($parts[7])) $parts[7] = '<i>'.$parts[7].'</i>';
if (isset($parts[13])) $parts[13] = '<u>'.$parts[13].'</u>';
$str = implode('', $parts);
$parts=preg_split(“/(示例)/”,$str,7,preg_split_DELIM_CAPTURE);
如果(isset($parts[3]))$parts[3]='.$parts[3].';
如果(isset($parts[7]))$parts[7]='.$parts[7].';
如果(isset($parts[13]))$parts[13]='.$parts[13].';
$str=内爆(“”,$parts);
第i次匹配的索引公式是index=i·2-1。谢谢Mario,我现在检查一下。。。只是为了确保$pattern='/[word]/'$subject=?,$matches=?我不知道该在subject和matches中放什么…那里的
$matches
只是一个未使用的一次性变量。但是$subject
是您的源文本。我已经这样实现了它$模式=“/”.$g_page_identified.“/”$主题=$g_页面内容2;//如果(7>=preg_match_all($pattern,$subject,$matches)){$cb_num=0;$subject=preg_replace_回调($pattern,“cb_ibu”,$subject);}函数cb_ibu($match){全局$cb_num;$match=$match[0];开关(+$cb num num){案例2:返回“$match”;案例4:返回“$match”;案例7:返回“$match”;默认值:返回$match;}}感谢mario的反馈,我很感激,我最终选择了GumboThanks mario提供的解决方案,我现在正在检查。。。只是为了确保$pattern='/[word]/'$subject=?,$matches=?我不知道该在subject和matches中放什么…那里的$matches
只是一个未使用的一次性变量。但是$subject
是您的源文本。我已经这样实现了它$模式=“/”.$g_page_identified.“/”$主题=$g_页面内容2;//如果(7>=preg_match_all($pattern,$subject,$matches)){$cb_num=0;$subject=preg_replace_回调($pattern,“cb_ibu”,$subject);}函数cb_ibu($match){全局$cb_num;$match=$match[0];开关(+$cb num num){案例2:返回“$match”;案例4:返回“$match”;案例7:返回“$match”;默认值:返回$match;}}谢谢mario的反馈,我很感激,我最终选择了GumboHanks Gumbo提供的解决方案!我几乎可以开始发送邮件了