PHP preg_replace替换文本,除非放在方括号内 [英] PHP preg_replace replace text unless inside brackets

查看:130
本文介绍了PHP preg_replace替换文本,除非放在方括号内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PHP的preg_replace()在文本中搜索某个单词的出现,并将该单词括在方括号中,除非已经存在方括号.这里的挑战是我要测试与我要查找的文本可能直接相邻或可能不直接相邻的方括号.

I would like to use PHP's preg_replace() to search a text for occurrences of a certain word, and enclose that word in brackets, unless there are already brackets present. The challenge here is that I want to test for brackets that may or may not be directly adjacent to the text I am looking for.

随机示例:我想将warfarin替换为[[warfarin]]

Random example: I want to replace warfarin with [[warfarin]]

  1. 在此字符串中:Use warfarin for the prevention of strokes
  2. 但在此字符串中:Use [[warfarin]] for the prevention of strokes(括号已存在)
  3. 该字符串中的
  4. :Use [[generic warfarin formulation]] for the prevention of strokes(已经存在远程"括号)
  1. in this string: Use warfarin for the prevention of strokes
  2. but not in this string: Use [[warfarin]] for the prevention of strokes (brackets already present)
  3. and not in this string either: Use [[generic warfarin formulation]] for the prevention of strokes ('remote' brackets already present)

我可以使用lookbehind和lookahead断言来满足前两个要求:

I can satisfy the first two requirements all right using lookbehind and lookahead assertions:

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

但是我需要您的第三项帮助,即在存在远程"括号时不要添加括号:

But I need your help with the third requirement, i.e. not adding brackets when there are 'remote' brackets present:

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,因为它包含在已经包含在方括号中的较长表达式中.

In this last example, the square brackets should not be added to the word warfarin since it is contained in a longer expression that is already enclosed in brackets.

问题在于PHP的regexp断言必须具有固定的长度,否则将非常简单.

The problem is that PHP's regexp assertions must have fixed length, otherwise it would be very simple.

我正在使用

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;

将导致

原文:

使用华法林预防中风.使用[[华法林]]预防中风.使用[[华法林通用制剂]]预防中风

Original:

Use warfarin for the prevention of strokes. Use [[warfarin]] for the prevention of strokes. Use [[generic warfarin formulation]] for the prevention of strokes

使用[[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 preg_replace替换文本,除非放在方括号内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆