preg_match_all
-
PHP returns an integer with the number of matches for
preg_match_alland populates a matches array by reference. Ruby performs multiple matches for a string using theString#scanmethod. This method returns a nested array of matches or an empty array when no matches are found. Be aware that the nesting of values in this array is different than howpreg_match_allorders matches.In this example, we match components of the email address, and both
preg_match_allandString#scangive us an array of matches that are found.PHP
$string = 'joe@example.com; walter@example.org'; $result = preg_match_all('/([a-z0-9_.-]+)@([a-z0-9-]+)\.[a-z.]+/i', $string, $matches); var_export($result); // => 2 var_export($matches); // => array(array('joe@example.com', 'walter@example.org'), // array('joe', 'walter'), // array('example', 'example'),
Ruby
string = 'joe@example.com; walter@example.org' result = string.scan(/([a-z0-9_.-]+)@([a-z0-9-]+)\.[a-z.]+/i) p result.size # => 2 p result # => [["joe", "example"], ["walter", "example"]]
see also
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.

