preg_split
-
We split strings by a pattern in Ruby using the
String#splitmethod. As withString#gsub, we can also use this same method to split using a string instead of a regular expression. This means thatsplitalso performs the equivalent of theexplodefunction in PHP.In this example, we create an array of the list of emails by splitting the string using the semi-colon and space as the delimiter.
PHP
$string = 'joe@example.com; walter@example.org'; $result = preg_split('/;\s?/', $string); var_export($result); // array('joe@example.com', 'walter@example.org')
Ruby
string = 'joe@example.com; walter@example.org' result = string.split(/;\s?/) p result # => ["joe@example.com", "walter@example.org"]
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.

