我想使用 PHP 的preg_replace()
在文本中搜索某个单词的出现,并将该单词括在括号中,除非已经存在括号。这里的挑战是,我想测试括号可能与我正在寻找的文本直接相邻,也可能不直接相邻。
随机示例:我想用[[warfarin]]
替换warfarin
- 在此字符串中:
Use warfarin for the prevention of strokes
- 但不在此字符串中:
Use [[warfarin]] for the prevention of strokes
(括号已存在( - 也不在此字符串中:
Use [[generic warfarin formulation]] for the prevention of strokes
("远程"括号已存在(
我可以使用后看断言和前瞻断言满足前两个要求:
php > echo preg_replace( "/(?<!'['[)(warfarin)(?!]])/", "[[$1]]", "Use warfarin for the prevention of strokes" );
Use [[warfarin]] for the prevention of strokes
php > echo preg_replace( "/(?<!'['[)(warfarin)(?!]])/", "[[$1]]", "Use [[warfarin]] for the prevention of strokes" );
Use [[warfarin]] for the prevention of strokes
但是我需要您对第三个要求的帮助,即当存在"远程"括号时不要添加括号:
php > echo preg_replace( "/(?<!'['[)(warfarin)(?!]])/", "[[$1]]", "Use [[generic warfarin formulation]] for the prevention of strokes" );
Use [[generic [[warfarin]] formulation]] for the prevention of strokes
在最后一个示例中,不应将方括号添加到单词 warfarin
中,因为它包含在已括在括号中的较长表达式中。
问题是 PHP 的正则表达式断言必须有固定的长度,否则它会非常简单。
我正在使用
PHP 5.3.10-1ubuntu3.1 with Suhosin-Patch (cli) (built: May 4 2012 02:20:36)
提前感谢!
这就是我会做的。
$str = 'Use warfarin for the prevention of strokes. ';
$str .= 'Use [[warfarin]] for the prevention of strokes. ';
$str .= 'Use [[generic warfarin formulation]] for the prevention of strokes';
$arr = preg_split('/('['[.*?']'])/',$str,-1,PREG_SPLIT_DELIM_CAPTURE);
// split the string by [[...]] groups
for ($i = 0; $i < count($arr); $i+=2) {
// even indexes will give plain text parts
$arr[$i] = preg_replace('/(warfarin)/i','[[$1]]',$arr[$i]);
// enclose necessary ones by double brackets
}
echo '<h3>Original:</h3>' . $str;
$str = implode('',$arr); // finally join them
echo '<h3>Changed:</h3>' . $str;
将导致
源语言:
使用华法林预防中风。使用[[华法林]]预防中风。使用[[华法林通用制剂]]预防中风
改变:
使用[[华法林]]预防中风。使用[[华法林]]预防中风。使用[[华法林通用制剂]]预防中风
试试这个:
echo preg_replace( "/(warfarin)([^']]+('[|$))/", "[[$1]]$2", "Use generic warfarin[[ formulation for]] the prevention of strokes'n" );
我假设不会有任何没有左括号的右括号的情况。