parsing textarea field: new line, comma, or semicolon

I have a textarea field for email addresses and allow separatation by comma, semicolon or new line.

parsing textarea: new line, comma, or semi colon

I use this to normalize line endings:

$email_field = preg_replace("/(\015\012\015\012)|(\015\015)|(\012\012)/","\n",$email_field);

Now i could explode() on "\n' and  loop through lines and explode on "," and ";"  but I was wondering if it was possible to turn this into a one-liner and have the results put in an array.

Who is Participating?
Harisha M GCommented:
Add one more choice, and it will be alright:

$emails = split("/\x0D|\x0D+|\x0A+|(\x0D\x0A)+|(\x0A\x0D)+|,+|;+|/",$email_field);
$email_field = preg_replace("/,|;/","\n",$email_field);

$emails = explode("\n",$email_field);
Harisha M GCommented:
How about this... You don't need even the normalization:

$email_field = <<<EMAILS;,

$emails = split("/\x0D+|\x0A+|(\x0D\x0A)+|(\x0A\x0D)+|,+|;+|/",$email_field);

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

drew22Author Commented:
close but it doesn't seem to work with single \r :   x0D   (chr 13)

$email_field = '' .chr(13) .''.chr(13) .  '';

I using this hack right now:
    $emails = explode('|', str_replace (array("\r\n", "\n", "\r",';',','), '|', $email_field));

drew22Author Commented:
is preg_spilt() better/faster than split?
Harisha M GCommented: says:

preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split()

So you can use preg_split instead of split()

Also, if you use that, this shorter code seems to work:

$emails = preg_split("/[\x0D\x0A+;,]+/",$email_field);

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.