如何仅返回带有preg_match或preg_match_all的命名组? [英] How to return only named groups with preg_match or preg_match_all?
本文介绍了如何仅返回带有preg_match或preg_match_all的命名组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
$string = "This is some text written on 2010-07-18.";
preg_match('|(?<date>\d\d\d\d-\d\d-\d\d)|i', $string, $arr_result);
print_r($arr_result);
返回:
Array
(
[0] => 2010-07-18
[date] => 2010-07-18
[1] => 2010-07-18
)
但我希望它是
Array
(
[date] => 2010-07-18
)
在PHP的PDO对象中,有一个选项可以通过删除这些重复的编号值来从数据库中过滤结果: PDO::FETCH_ASSOC
.但是我还没有看到PHP中PCRE函数的类似修饰符.
In PHP's PDO object there is an option that is filtering results from database by removing these duplicate numbered values : PDO::FETCH_ASSOC
. But I haven't seen similar modifier for the PCRE functions in PHP yet.
推荐答案
我不认为您可以使preg_*
做到这一点,但是您可以通过一个简单的循环来做到这一点.但是我不明白为什么这些元素会带来问题.
I do not think you can make preg_*
do it, but you can do it with a simple loop. But I don't see why those elements pose a problem.
这篇关于如何仅返回带有preg_match或preg_match_all的命名组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文