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\0 15\012)|(\ 015\015)|( \012\012)/ ","\n",$em ail_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.
parsing textarea: new line, comma, or semi colon
I use this to normalize line endings:
$email_field = preg_replace("/(\015\012\0
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.
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.c om
EMAILS;
$emails = split("/\x0D+|\x0A+|(\x0D\ x0A)+|(\x0 A\x0D)+|,+ |;+|/",$em ail_field) ;
print_r($emails);
?>
<?php
$email_field = <<<EMAILS
hello@gmail.com
abcd@yahoo.com;xyz@aol.com
EMAILS;
$emails = split("/\x0D+|\x0A+|(\x0D\
print_r($emails);
?>
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));
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
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+;,]
$emails = explode("\n",$email_field)