Solved

parsing textarea field: new line, comma, or semicolon

Posted on 2007-04-07
6
1,925 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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