Link to home
Start Free TrialLog in
Avatar of drew22
drew22

asked on

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.

Avatar of steelseth12
steelseth12
Flag of Cyprus image

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

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

<?php
$email_field = <<<EMAILS
hello@gmail.com

abcd@yahoo.com;xyz@aol.com,pqr@msn.com
EMAILS;

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

print_r($emails);
?>
Avatar of drew22
drew22

ASKER

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

$email_field = 'abcd@yahoo.com' .chr(13) .' xyz@aol.com'.chr(13) .  'pqr@msn.com';

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

ASKER CERTIFIED SOLUTION
Avatar of Harisha M G
Harisha M G
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of drew22

ASKER

is preg_spilt() better/faster than split?
http://php.net/split 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);