在关键字PHP之前至少获取X个字符
我正在使用php和mysql制作一个小型搜索系统 我有这个:在关键字PHP之前至少获取X个字符,php,mysql,regex,Php,Mysql,Regex,我正在使用php和mysql制作一个小型搜索系统 我有这个: preg_match('#(.{75}' . $s . '.{75})#s', $b, $match); if (isset($match[1])) { return preg_replace('#(.+?)' . $s . '(.+)#s', '$1<span><b>' . $s . '</b> </span>$2', $match[1]); } else { r
preg_match('#(.{75}' . $s . '.{75})#s', $b, $match);
if (isset($match[1])) {
return preg_replace('#(.+?)' . $s . '(.+)#s', '$1<span><b>' . $s . '</b> </span>$2', $match[1]);
} else {
return 'Error';
}
preg#u match('#(.{75}.$s.{75})#s',$b,$match);
如果(isset($match[1])){
返回preg#u replace(“#(.+?)”.$s.“(.+)#s',“$1”。$s.'$2',$match[1]);
}否则{
返回“错误”;
}
这可以很好地获得关键字的第一次出现,并在其前后获得75个字符。唯一的问题是,如果少于75个字符,它将无法通过。我对regex很陌生,我实际上在上面的代码上得到了帮助,但它不是我的。如果我正确理解了从
n
数字到75的任何字符的匹配,如果是这样的话,你所要做的就是:{10,75}
其中10
是regex中的n
数字(下限)
'#(.{10,75}' . $s . '.{10,75})#s'
如果你想自己学习如何使用正则表达式,你应该试着自己找到答案。在这种情况下,并不是那么困难。也可以考虑使用<代码> PrggExpRe/<代码>第五参数>代码> $计数>代码>,而不是运行正则表达式两次。