Solved

parsing textarea field: new line, comma, or semicolon

Posted on 2007-04-07
6
1,931 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to count occurrences of each item in an array.

688 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