php正则表达式帮助替换特定符号内的文本

php正则表达式帮助替换特定符号内的文本,php,regex,Php,Regex,我有一个字符串,其中一个是 [my_name] and another being <my_name> 考虑到这将查找[]标记中的任何内容,我认为以下方法应该有效: preg_replace('/([\[<])[^\]>]+([\]>])/', "$1BOB$2", $str); preg\u replace(“/([\[]+([\]>])/”、“$1BOB$2”、$str); 正则表达式的解释: ([\[<]) -> First capturin

我有一个字符串,其中一个是

[my_name] and another being <my_name>

考虑到这将查找[]标记中的任何内容,我认为以下方法应该有效:

preg_replace('/([\[<])[^\]>]+([\]>])/', "$1BOB$2", $str);
preg\u replace(“/([\[]+([\]>])/”、“$1BOB$2”、$str);
正则表达式的解释:

([\[<]) -> First capturing group. Here we describe the starting characters using
           a character class that contains [ and < (the [ is escaped as \[)
[^\]>]+ -> The stuff that comes between the [ and ] or the < and >. This is a
           character class that says we want any character other than a ] or >.
           The ] is escaped as \].
([\]>]) -> The second capturing group. We we describe the ending characters using
           another character class. This is similar to the first capturing group.
([\[第一个捕获组。这里我们使用
包含[和<(将[转义为\[)的字符类
[^\]>]+->介于[和]或<和>之间的内容。这是一个
字符类,表示我们需要除]或>以外的任何字符。
]被转义为\]。
([\]>])->第二个捕获组。我们使用
另一个角色类。这类似于第一个捕获组。

替换模式使用反向引用来引用捕获组。
$1
表示第一个捕获组,它可以包含
[

,我认为以下方法应该可以工作:

preg_replace('/([\[<])[^\]>]+([\]>])/', "$1BOB$2", $str);
preg\u replace(“/([\[]+([\]>])/”、“$1BOB$2”、$str);
正则表达式的解释:

([\[<]) -> First capturing group. Here we describe the starting characters using
           a character class that contains [ and < (the [ is escaped as \[)
[^\]>]+ -> The stuff that comes between the [ and ] or the < and >. This is a
           character class that says we want any character other than a ] or >.
           The ] is escaped as \].
([\]>]) -> The second capturing group. We we describe the ending characters using
           another character class. This is similar to the first capturing group.
([\[第一个捕获组。这里我们使用
包含[和<(将[转义为\[)的字符类
[^\]>]+->介于[和]或<和>之间的内容。这是一个
字符类,表示我们需要除]或>以外的任何字符。
]被转义为\]。
([\]>])->第二个捕获组。我们使用
另一个角色类。这类似于第一个捕获组。
替换模式使用反向引用来引用捕获组。
$1
表示第一个捕获组,它可以包含
[

$str=“[my_name]和另一个存在”;
$replace=“BOB”;
preg_replace('/([\[])/i',“$1.”,$replace.“$2”,$str);
$str=“[my_name]and other being”;
$replace=“BOB”;
preg_replace('/([\[])/i',“$1.”,$replace.“$2”,$str);

您想使用
preg\u replace\u callback
这里是一个快速示例

$template = "Hello [your_name], from [my_name]";
$data = array(
    "your_name"=>"Yevo",
    "my_name"=>"Orangepill"
);

$func = function($matches) use ($data) {
    print_r($matches);
    return $data[$matches[1]];
};

echo preg_replace_callback('/[\[|<](.*)[\]\)]/U', $func, $template);
$template=“你好[你的名字],来自[我的名字]”;
$data=数组(
“你的名字”=>“Yevo”,
“我的名字”=>“橘子球”
);
$func=函数($matches)使用($data){
打印(匹配项);
返回$data[$matches[1]];
};

echo preg\u replace\u callback('/[\[\124;您想使用
preg\u replace\u callback
这里是一个快速示例

$template = "Hello [your_name], from [my_name]";
$data = array(
    "your_name"=>"Yevo",
    "my_name"=>"Orangepill"
);

$func = function($matches) use ($data) {
    print_r($matches);
    return $data[$matches[1]];
};

echo preg_replace_callback('/[\[|<](.*)[\]\)]/U', $func, $template);
$template=“你好[你的名字],来自[我的名字]”;
$data=数组(
“你的名字”=>“Yevo”,
“我的名字”=>“橘子球”
);
$func=函数($matches)使用($data){
打印(匹配项);
返回$data[$matches[1]];
};


回显preg\u replace\u回调('/[\[|你有没有试过学习一点如何使用正则表达式?@Jerry,冷静点。这样的问题对一些人来说可能是一个挑战。我发现StackOverflow也是一个提高自己技能的好地方。@Jari如果你不知道从哪里开始使用正则表达式,那么你至少应该试着学习一些东西。因此,一些显示出你努力的问题当然,我100%同意你的看法。但事实上,这个问题对其他初学者可能也很有用。@Yevo你能告诉我们你做过什么吗?不是我们不想帮忙,而是正如我前面提到的,这样,那些在OP方面表现出一定努力的问题会得到更好的回答。你有没有想过你甚至试着学习一点如何使用正则表达式?@Jerry,冷静点。这样的问题对某些人来说可能是一个挑战。我发现StackOverflow也是一个提高自己技能的好地方。@Jari如果你不知道从哪里开始使用正则表达式,那么你至少应该尝试学习一些东西。因此,在pa上显示出一些努力的问题OP的rt更受欢迎。@VivinPalath当然,我100%同意你的观点。但事实上,这个问题可能对其他初学者也很有用。@Yevo你能告诉我们你尝试过什么吗?不是我们不想帮忙,而是正如我前面提到的,这样,OP部分表现出一些努力的问题会受到更好的欢迎。我补充了en丁格定界符。我添加了结尾定界符。我认为这行不通。这将替换与
BOB
匹配的任何内容。我认为这行不通。这将替换与
BOB
匹配的任何内容。为什么不修改原始答案而不是添加新答案?我将删除旧答案…保留它,因为我认为它是正确的可能对某人有用。为什么不修改原始答案而不是添加新答案?我将删除旧答案…保留它,因为我认为它可能对某人有用。