Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带preg_replace的复杂正则表达式,替换不在[and]内的单词_Php_Regex_Preg Replace - Fatal编程技术网

Php 带preg_replace的复杂正则表达式,替换不在[and]内的单词

Php 带preg_replace的复杂正则表达式,替换不在[and]内的单词,php,regex,preg-replace,Php,Regex,Preg Replace,我很难找到一个正确的正则表达式来实现我想要的 我有这样一句话: Hi, my name is Stan, you are welcome, hello. [hi|hello|welcome], my name is [stan|jack] you are [hi|hello|welcome] [hi|hello|welcome]. 我想把它变成这样: Hi, my name is Stan, you are welcome, hello. [hi|hello|welcome], my na

我很难找到一个正确的正则表达式来实现我想要的

我有这样一句话:

Hi, my name is Stan, you are welcome, hello.
[hi|hello|welcome], my name is [stan|jack] you are [hi|hello|welcome] [hi|hello|welcome].
我想把它变成这样:

Hi, my name is Stan, you are welcome, hello.
[hi|hello|welcome], my name is [stan|jack] you are [hi|hello|welcome] [hi|hello|welcome].
现在我的正则表达式工作了一半,因为有些单词没有被替换,而那些被替换的单词正在删除一些字符

这是我的测试代码

<?php 

$test = 'Hi, my name is Stan, you are welcome, hello.';

$words = array(
    array('hi', 'hello', 'welcome'),
    array('stan', 'jack'),
);

$result = $test;
foreach ($words as $group) {
    if (count($group) > 0) {
        $replacement = '[' . implode('|', $group) . ']';
        foreach ($group as $word) {
            $result = preg_replace('#([^\[])' . $word . '([^\]])#i', $replacement, $result);
        }
    }
}

echo $test . '<br />' . $result;
支持数组作为参数。不需要使用循环进行迭代

$s = array("/(hi|hello|welcome)/i", "/(stan|jack)/i");
$r = array("[hi|hello|welcome]", "[stan|jack]");
preg_replace($s, $r, $str); 
动态地

$test = 'Hi, my name is Stan, you are welcome, hello.';
$s = array("hi|hello|welcome", "stan|jack");
$r = array_map(create_function('$a','return "[$a]";'), $s);
$s = array_map(create_function('$a','return "/($a)/i";'), $s);
echo preg_replace($s, $r, $str);
//[hi|hello|welcome], my name is [stan|jack], you are [hi|hello|welcome], [hi|hello|welcome].

您的正则表达式:

'#([^\[])' . $word . '([^\]])#i'
$word
前后也匹配一个字符。当他们这样做的时候,他们取代了它。因此,您的替换字符串也需要引用这些部分:

'$1' . $replacement . '$2'

您使用的正则表达式过于复杂。只需使用正则括号()进行正则表达式替换:


hi此选项效果更好,但是第一个单词(hi)不是replacedSure,因为它没有前面的字符(您正在使用正则表达式查找)。因此,请查找not
[
|
^
(字符串的开头)。结尾相同。请参阅