我在PHP中使用替换功能替换单词并在其下划线,
$replace='<span style="font-weight:bold; border-bottom: 1px solid #999999;" >'.$term.'</span>';
$post = str_ireplace($term, $replace, $post,$count);
问题是,如果匹配的单词是are
,而主题是care
,则它只是在下划线are
而不是下划线c
,其他任何方式都可以使整个单词下划线。
$replace='<span style="font-weight:bold; border-bottom: 1px solid #999999;" >$0</span>';
$post = preg_replace("/[a-z]*{$term}[a-z]*/i", $replace, $post);
[a-z]*
匹配任何字母序列,因此这将包括匹配项中匹配项周围的其余单词。$1
in$replace
替换为与正则表达式匹配的任何内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句