php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54279 preg_grep does not recognize this regex '/[0-9]*$/' to filter numbers out
Submitted: 2011-03-16 23:26 UTC Modified: 2011-03-16 23:42 UTC
From: donciprian at gmail dot com Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.3SVN-2011-03-16 (SVN) OS: Ubuntu 10.10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: donciprian at gmail dot com
New email:
PHP Version: OS:

 

 [2011-03-16 23:26 UTC] donciprian at gmail dot com
Description:
------------
I am trying to filter numbers from the end of a string in a array.

i have a array full of these strings :

$testArr = array('foto_1','foto_45');

$resArr = preg_grep('/[0-9]*$/',$testArr);

and the $resArr array that gets returned from preg_grep, contains the same
strings, i am looking for a array that contains just the numbers that were 
filtered using the regex



Test script:
---------------
<?php

$testArr = array('foto_1','foto_45');

$resArr = preg_grep('/[0-9]*$/',$testArr);

print_r($resArr);

?>

Expected result:
----------------
to see a array full of the numbers what were at the end of the string in the 
input array

something like this :

Array
(
    [0] => 1
    [1] => 45
)

Actual result:
--------------
Array
(
    [0] => foto_1
    [1] => foto_45
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-16 23:42 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2011-03-16 23:42 UTC] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

There is no bug here... preg_grep is not meant to return the group captured data separately... You need to use preg_match()/preg_match_all() for such.
 [2011-03-16 23:56 UTC] donciprian at gmail dot com
oops, my mistake ;)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 11:01:34 2025 UTC