preg_grep
-
The
preg_grepfunction in PHP is a useful function to find entries in an array that match a given pattern. Ruby does this same operation with thegrepmethod.In this example, we’ll build a new array that only consists of email addresses that end in
.com.PHP
$myArray = array('joe@example.com', 'walter@example.org'); $result = preg_grep('/\.com$/', $myArray); var_export($result); // => array('joe@example.com')
Ruby
my_array = ['joe@example.com', 'walter@example.org'] result = my_array.grep(/\.com$/) p result # => ["joe@example.com"]
Looking for Rails or PHP web application development, integration, and training?
Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.

