这里是我的代码:当我们有一个子字符串时如何匹配整个单词?
$txt = 'this is a text';
$word = 'is';
echo str_replace($word, '<b>'.$word.'</b>', $txt);
//=> th<b>is</b> <b>is</b> a text
正如你看到的,我的子字符串是上面的例子中is
,它只是is
的this
部分匹配。虽然我需要选择整个单词。因此,这是预期的结果:
//=> <b>this</b> <b>is</b> a text
所以我需要检查子串的左侧和右侧,符合一切,直到第一个字符串^
或字符串$
或白色S页面\s
月底。
我该怎么做?
那么你可以用String#的indexOf,然后知道使用适当子建的包装。 – Rogue