Page 594 - Beginning PHP 5.3
P. 594

Part III: Using PHP in Practice
                  Searching Arrays with preg_grep()

                    preg_match()  and  preg_match_all()  search individual strings of text. If you want to search an entire
                 array of strings, you can use   preg_grep() . This function takes three arguments  —  the regular
                 expression, the array of strings, and optional flags  —  and returns an array containing the array elements
                 that matched the expression, keyed by the elements ’  indices in the original array. Here ’ s an example:

                    $text = array(
                     “His three whales are as good whales as were ever published in”,
                     “Wapping, at any rate; and his stump as unquestionable a stump”,
                     “as any you will find in the western clearings.”
                    );

                    $results =  preg_grep( “/\bin\b/”, $text );
                    echo “ < pre > ”;
                    print_r( $results );
                    echo “ < /pre > ”;

                   This code searches for the word  “ in ”  within the strings in the  $text  array, and produces the
                 following output:

                    Array
                    (
                        [0] = >  His three whales are as good whales as were ever published in
                        [2] = >  as any you will find in the western clearings.

                    )
                   If you ’ d prefer to get a list of elements that  don ’ t  match the pattern, pass the  PREG_GREP_INVERT  flag as
                 the third argument to   preg_grep() :

                    $text = array(
                     “His three whales are as good whales as were ever published in”,
                     “Wapping, at any rate; and his stump as unquestionable a stump”,
                     “as any you will find in the western clearings.”
                    );

                    $results =  preg_grep( “/\bin\b/”, $text, PREG_GREP_INVERT );
                    echo “ < pre > ”;
                    print_r( $results );

                    echo “ < /pre > ”;
                   This code displays:
                    Array
                    (
                        [1] = >  Wapping, at any rate; and his stump as unquestionable a stump

                    )
                    preg_grep()  doesn ’ t give you much detail, such as the actual matched text or how many times the text
                 matched, but it ’ s great for quickly reducing a large array of text strings  —  such as that returned from a
                 database query  —  down to just the strings that match. You can then perform a more fine - grained search
                 on the matched strings, if required.


              556





                                                                                                      9/21/09   6:17:58 PM
          c18.indd   556
          c18.indd   556                                                                              9/21/09   6:17:58 PM
   589   590   591   592   593   594   595   596   597   598   599