Home » PHP/MySQL, Programming Techniques, Regular Expression » PHP – Search in an Array for Values Matching a Pattern – Regex, Wildcard

I have an array with many values and I need to do a search to find all the values that match a pattern. We have functions like in_array & array_search in PHP, but these functions basically try to match the exact needle value in the array. I need to use my Regular Expression Pattern and find all the array values that match the regex pattern.

The PHP function preg_grep handles this beautifully. It accepts the Regex pattern and the array to search for as its parameters. It then returns the array consisting of the elements of the input array that match the given pattern. The returned array is indexed using the keys from the input array.

Here is my array:
Array
(
[0] => Armenia
[1] => America
[2] => Algeria
[3] => India
[4] => Brazil
[5] => Croatia
[6] => Denmark
)
I want to find all the countries in the array which start with the letter ‘A’. We need to form a regular expression which will match all the strings starting with letter A.

I have got this simple regular expression: ‘/^A.*/’

Now here is the PHP code to find the values from the Array.

<?php
$array = array('Armenia', 'America', 'Algeria', 'India', 'Brazil', 'Croatia', 'Denmark');
$fl_array = preg_grep('/^A.*/', $array);
echo '<pre>';
print_r($fl_array);
echo '</pre>';
?>

Which then gives you this output:
Array
(
[0] => Armenia
[1] => America
[2] => Algeria
)

Here are some Regular Expression Patterns you could use.

Find whole numbers: ‘/^\d+$/’
Floating numbers: ‘/^\d+\.{1}\d+$/’
Lowercase Words: ‘/^[a-z]+$/’

Play with Regular Expressions and let me know if you have any questions or if you need more patterns. I am planning to publish an article on Regular Expressions soon.

10 Comments

  1. Ravi says:

    How can we use the same code for multi dimensional array?

  2. Tino says:

    Hey Aneeska,

    thx for your article. It helped me a lot. I’m very much interested in further search patterns. For example:

    Array values:

    table
    table_2
    tableothertable

    I would like a search pattern, that only gives me the values “table” and table_2″. Is that possible?

  3. Aneeska says:

    Hi Tino,

    ‘/table_?(\d{1})*$/’

    Try this. This will match ‘table’, ‘table_’, ‘table_1′ (or any one digit number). It will not match ‘tablesometext’

    Or,
    ‘/table(_2)?$/’
    This will match exactly what you want. only ‘table’ and ‘table_2′ and nothing else.

    Thanks,
    Anees

  4. Ted says:

    Nice, but do you have a version to wildcard search array keys?

  5. Mat says:

    Hi
    Im trying to search for nn/nnnnnn/nn in a file..

    I have this

    if (preg_match_all(“([0-9]{6})”, $file, $out)) {
    echo “Match was found “;
    print_r($out);

    which deals with the centre 6 numbers but how do I get it to search for the whole pattern?
    Cheers
    Mat

  6. Aneeska says:

    if(preg_match_all(“([0-9]{2}/[0-9]{6}/[0-9]{2})”, $str, $out)) {
    echo “Match was found “;
    print_r($out);
    }

  7. aaslesh says:

    How can i write the output into a file

  8. bernhard says:

    thank you for this article! :)

  9. Aneeska says:

    Aaslesh,

    file_put_contents(‘/tmp/somefile.txt’, print_r($fl_array, true));

Leave a Reply

Page optimized by WP Minify WordPress Plugin