[英]Named capture in PHP using regex
How can I use named capture with regex in PHP? 如何在PHP中将命名捕获与regex一起使用? Can anyone give me a working example?
谁能给我一个可行的例子?
Doesn't work with replace , only useful for match in php 不适用于replace,仅对php中的匹配有用
$test="yet another test";
preg_match('/(?P<word>t[^s]+)/',$test,$matches);
var_dump($matches['word']);
According documentation 根据文件
PHP 5.2.2 introduced two alternative syntaxes
(?<name>pattern)
and(?'name'pattern)
PHP 5.2.2引入了两种替代语法
(?<name>pattern)
和(?'name'pattern)
So you'll get same result using: 因此,您将使用以下方法获得相同的结果:
<?php
preg_match('/(?P<test>.+)/', $string, $matches); // basic syntax
preg_match('/(?<test>.+)/', $string, $matches); // alternative
preg_match("/(?'test'.+)/", $string, $matches); // alternative
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.