如何替换完全匹配或部分匹配的完整单词
How to replace full words that are either fully or part matched
我正在尝试使用 preg_replace 或 str_ireplace 来包装跨度 class 标记找到的关键字,但是使用 str_ireplace 它将把 'wooding' 等单词切成两半,例如:
<span class="highlight">wood</span>ing
这是一个针、大海捞针和必需品的例子return:
针数:
wood
干草堆:
wood and stuff
this doesnt contain the keyword
Wooding is what we do
我想要什么 returned:
<span class="highlight">wood</span> and stuff
this doesnt contain the keyword
<span class="highlight">Wooding</span> is what we do
这是我的 preg_replace 实验的 link:
http://www.phpliveregex.com/p/i4m
您需要在结束词边界之前使用带有可选字符的正则表达式和词边界,以确保它正是您要查找的词。尝试:
$string = 'wood and stuff
this doesnt contain the keyword
Wooding is what we do';
echo preg_replace('/\b(wood[a-z]*)\b/i', '<span class="highlight"></span>', $string);
PHP 演示:https://eval.in/689239
正则表达式演示:https://regex101.com/r/f9B6mL/1
对于多个术语,您可以使用非捕获组和 |
进行术语分隔。
$string = 'wood and metal stuff
this doesnt contain the keyword
Wooding is what we do metals';
echo preg_replace('/\b((?:wood|metal)[a-z]*)\b/i', '<span class="highlight"></span>', $string);
试试这个正则表达式:\b(wood.*?)\b
,它匹配以 wood
开头后跟任意数量的单词字符的单词。
$intput = 'put your input here';
$result = preg_replace(/\b(wood.*?)\b/i, '<span class="highlight">\1</span>', $input);
怎么样:
$str = <<<EOD
wood and stuff
this doesnt contain the keyword
Wooding is what we do
EOD;
$needle = 'wood';
$str = preg_replace("/\w*$needle\w*/is", '<span class="highlight">[=10=]</span>', $str);
echo $str,"\n";
输出:
<span class="highlight">wood</span> and stuff
this doesnt contain the keyword
<span class="highlight">Wooding</span> is what we do
我正在尝试使用 preg_replace 或 str_ireplace 来包装跨度 class 标记找到的关键字,但是使用 str_ireplace 它将把 'wooding' 等单词切成两半,例如:
<span class="highlight">wood</span>ing
这是一个针、大海捞针和必需品的例子return:
针数:
wood
干草堆:
wood and stuff
this doesnt contain the keyword
Wooding is what we do
我想要什么 returned:
<span class="highlight">wood</span> and stuff
this doesnt contain the keyword
<span class="highlight">Wooding</span> is what we do
这是我的 preg_replace 实验的 link: http://www.phpliveregex.com/p/i4m
您需要在结束词边界之前使用带有可选字符的正则表达式和词边界,以确保它正是您要查找的词。尝试:
$string = 'wood and stuff
this doesnt contain the keyword
Wooding is what we do';
echo preg_replace('/\b(wood[a-z]*)\b/i', '<span class="highlight"></span>', $string);
PHP 演示:https://eval.in/689239
正则表达式演示:https://regex101.com/r/f9B6mL/1
对于多个术语,您可以使用非捕获组和 |
进行术语分隔。
$string = 'wood and metal stuff
this doesnt contain the keyword
Wooding is what we do metals';
echo preg_replace('/\b((?:wood|metal)[a-z]*)\b/i', '<span class="highlight"></span>', $string);
试试这个正则表达式:\b(wood.*?)\b
,它匹配以 wood
开头后跟任意数量的单词字符的单词。
$intput = 'put your input here';
$result = preg_replace(/\b(wood.*?)\b/i, '<span class="highlight">\1</span>', $input);
怎么样:
$str = <<<EOD
wood and stuff
this doesnt contain the keyword
Wooding is what we do
EOD;
$needle = 'wood';
$str = preg_replace("/\w*$needle\w*/is", '<span class="highlight">[=10=]</span>', $str);
echo $str,"\n";
输出:
<span class="highlight">wood</span> and stuff
this doesnt contain the keyword
<span class="highlight">Wooding</span> is what we do