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

I want to sort an array in reverse order of values in php

php arrays

I want to sort an array in reverse order of values. I used arsort php function but the result is not good for me.

Searching a scala caching framework with annotation like (spring + ehcache)

spring scala caching guava ehcache

I'm at the moment using spring AOP + Ehcache for our scala application. I really like the simplicity of using it with annotation but the not the part of using a big framework like spring to perform

join two tables into one big table

sql database

I have two tables with the same columns, and I need to copy one table's rows to the other table's rows to create one big table with all the values from both tables. Right now I am doing this query...