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

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

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

Expert Comment

by:Harisha M G
ID: 18871898
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

ID: 18872746
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));

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 37

Accepted Solution

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

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

Author Comment

ID: 18872850
is preg_spilt() better/faster than split?
LVL 37

Expert Comment

by:Harisha M G
ID: 18872868 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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