开发者

Preg_replace problem, must be trivial [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the hel开发者_StackOverflow社区p center. Closed 12 years ago.

I am obviously doing something completely wrong when it comes to preg_replace.

Say I have a string of text,

"Silence is golden until you are the one silenced." And I want to replace "Silence" with "Silence" but not replace the silence in silenced.

Here is the code I have in place:

$q = "Silence is golden until you are the one silenced.";
$card = "Silence";
$pattern = '/\b'.$card.'\b/i';
$q = preg_replace($pattern,'<strong>'.$card.'</strong>',$q);

This is failing and setting $q = ""

What am I doing wrong?


Problem solved. In the actual code I am iterating over an array of items to preg_replace and just applying preg_replace without first testing using preg_match.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜