Solved

parsing textarea field: new line, comma, or semicolon

Posted on 2007-04-07
6
1,915 Views
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.

0
Comment
Question by:drew22
  • 3
  • 2
6 Comments
 
LVL 20

Expert Comment

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

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

Expert Comment

by:Harisha M G
ID: 18871898
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);
?>
0
 
LVL 1

Author Comment

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

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 37

Accepted Solution

by:
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);
0
 
LVL 1

Author Comment

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

Expert Comment

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

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

815 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

11 Experts available now in Live!

Get 1:1 Help Now