【发布时间】:2010-05-12 05:21:13
【问题描述】:
考虑以下字符串:
这是一个字符串,其中一些关键字 可用。 '我需要格式化 来自 STRING 的关键字
在上面的字符串关键字是STRING和WHERE
现在我需要得到如下输出:
this is a <b>STRING</b> <b>WHERE</b> some keywords ARE available. 'i need TO format the KEYWORDS from the STRING'
这样 html 输出会是这样的:
这是一个STRING WHERE一些关键字 可用。 '我需要格式化 来自 STRING 的关键字
请注意,引号 ('...') 字符串中的关键字将被忽略。在上面的示例中,我忽略了引用字符串中的 STRING 关键字。
请提供以下 PHP 脚本的修改版本,以便我可以得到我想要的结果:
$patterns = array('/STRING/','/WHERE/');
$replaces = array('<b>STRING</b>', '<b>WHERE</b>');
$string = "this is a STRING WHERE some keywords ARE available. 'i need TO format the KEYWORDS from the STRING'";
preg_replace($patterns, $replaces, $string);
【问题讨论】:
标签: php string replace preg-replace design-patterns