splitting multi-line email address entries

I'm having problems with splitting an array correctly. I have a textarea input that allows for multiple email addresses to be entered one per line. Once entered I'm turning this into an array and then adding them to a database. Code below:

$addrs = explode('<br />',nl2br($form['bulk_add_emails']));

This is still leaving a linebreak in the database entry before the email address, which means when I run a validation check using

if(preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $v)){ ... }

everything is returning as false.

What am I doing wrong and how can I clean up the data correctly?

Answers


Don't explode() on <br /> and don't call nl2br(). Instead, use preg_split() for more flexibility in processing the linebreaks, which can handle multiple lines if there's an empty line, and \r\n or \n linebreaks.

// (\r?\n)+ will split on one or more \n with an optional \r preceding it.
// ... so empty lines don't result in empty array elements.
$addrs = preg_split('/(\r?\n)+/', $form['bulk_add_emails']);

Need Your Help

Python raw http request retrieval issues via urllib2

python http request

I am using Python 2.6.5 and I am trying to capture the raw http request sent via HTTP, this works fine except when I add a proxy handler into the mix so the situation is as follows:

JSF / Applet integration

jsf jsf-2 applet

In our current prototype most the standard HTML controls are replaced by an applet, most important the form submission is triggered by the applet.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.