Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Latest commit

 

History

History
27 lines (18 loc) · 618 Bytes

preg_grep.markdown

File metadata and controls

27 lines (18 loc) · 618 Bytes

preg_grep

The preg_grep function in PHP is a useful function to find entries in an array that match a given pattern. Ruby does this same operation with the grep method.

In this example, we'll build a new array that only consists of email addresses that end in .com.

{{code:php $myArray = array('[email protected]', '[email protected]'); $result = preg_grep('/.com$/', $myArray);

var_export($result);
// => array('[email protected]')

}}

{{code:ruby my_array = ['[email protected]', '[email protected]'] result = my_array.grep(/.com$/)

p result
# => ["[email protected]"]

}}