parsing textarea field: new line, comma, or semicolon

Posted on 2007-04-07
Last Modified: 2012-06-27
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.

Question by:drew22
  • 3
  • 2
LVL 20

Expert Comment

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

$emails = explode("\n",$email_field);
LVL 37

Expert Comment

by:Harisha M G
Comment Utility
How about this... You don't need even the normalization:

$email_field = <<<EMAILS;,

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


Author Comment

Comment Utility
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));

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

LVL 37

Accepted Solution

Harisha M G earned 500 total points
Comment Utility
Add one more choice, and it will be alright:

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

Author Comment

Comment Utility
is preg_spilt() better/faster than split?
LVL 37

Expert Comment

by:Harisha M G
Comment Utility 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);


Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now