12

很简单;我似乎找不到任何关于 PHPpreg_replace()支持命名反向引用的明确信息:

// should match, replace, and output: user/profile/foo
$string = 'user/foo';
echo preg_replace('#^user/(?P<id>[^/]+)$#Di', 'user/profile/(?P=id)', $string);

这是一个简单的示例,但我想知道是否(?P=name)根本不支持这种语法。语法问题,还是不存在的功能?

4

4 回答 4

14

它们存在:

http://www.php.net/manual/en/regexp.reference.back-references.php

使用 preg_replace_callback:

function my_replace($matches) {
    return '/user/profile/' . $matches['id'];
}
$newandimproved = preg_replace_callback('#^user/(?P<id>[^/]+)$#Di', 'my_replace', $string);

甚至更快

$newandimproved = preg_replace('#^user/([^/]+)$#Di', '/user/profile/$1', $string);
于 2011-03-10T03:38:59.313 回答
7

preg_replace不支持命名反向引用。

preg_replace_callback支持命名反向引用,但在 PHP 5.3 之后,所以预计它在 PHP 5.2 及更低版本上会失败。

于 2011-03-10T03:41:07.243 回答
2

preg_replace还不支持命名子模式。

于 2011-03-10T03:40:52.440 回答
0

你可以使用这个:

class oreg_replace_helper {
    const REGEXP = '~
(?<!\x5C)(\x5C\x5C)*+
(?:
    (?:
        \x5C(?P<num>\d++)
    )
    |
    (?:
        \$\+?{(?P<name1>\w++)}
    )
    |
    (?:
        \x5Cg\<(?P<name2>\w++)\>
    )
)?
~xs';

    protected $replace;
    protected $matches;

    public function __construct($replace) {
        $this->replace = $replace;
    }

    public function replace($matches) {
        var_dump($matches);
        $this->matches = $matches;
        return preg_replace_callback(self::REGEXP, array($this, 'map'), $this->replace);
    }

    public function map($matches) {
        foreach (array('num', 'name1', 'name2') as $name) {
            if (isset($this->matches[$matches[$name]])) {
                return stripslashes($matches[1]) . $this->matches[$matches[$name]];
            }
        }
        return stripslashes($matches[1]);
    }
}

function oreg_replace($pattern, $replace, $subject) {
    return preg_replace_callback($pattern, array(new oreg_replace_helper($replace), 'replace'), $subject);
}

\g<name> ${name} or $+{name}那么你可以在你的替换语句中使用任何一个作为参考。

cf(http://www.rexegg.com/regex-disambiguation.html#namedcapture

于 2015-05-12T16:48:12.817 回答