views:

54

answers:

2

example:

<?php
$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);
?>

returns:

Array
(
    [0] => 2010-07-18
    [date] => 2010-07-18
    [1] => 2010-07-18
)

but I want it to be:

Array
(
    [date] => 2010-07-18
)

in php's pdo object there is an option that is filtering results from database by removing these duplicate numbered values : PDO::FETCH_ASSOC (http://www.php.net/manual/en/pdostatement.fetch.php). But I haven't seen similar modifier for the pcre functions in php yet.

A: 

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.

Maerlyn
In small arrays this not pose a problem but in very big with lots of patterns there might be problems with high memory needs.Second, it looks cleaner that way, and there would be (with this non existent modifier) one line of code less, so less possibility of an error.
Quite the opposite: because this can't be done by PCRE, you'd have to add more code, where there could be more problems.
Charles
I meant the additional elements in the array.
Maerlyn
What the opposite, if this modifier i ask for would exist then that extra line wouldn't be needed, it would be even better if it would be a global modifier that would set preferences of all pcre functions setting mode for output arraysI repeat it: it was done in PDO object, what it can't be done in pcre ?
as you said: PDO object-based, pcre is still function-based. They'll possibly change it some time later, as the language evolves.
Maerlyn
A: 

$string = "This is some text written on 2010-07-18."; preg_match('|(?\d\d\d\d-\d\d-\d\d)|i',$string,$arr_result); echo $arr_result['date'];

Rahul A
no, that is wrong, I meant a modifier to automatically delete keys that are numbers, it's not that easy on multibranched array that preg_match_all might produce