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?
Don’t
explode()on<br />and don’t callnl2br(). Instead, usepreg_split()for more flexibility in processing the linebreaks, which can handle multiple lines if there’s an empty line, and\r\nor\nlinebreaks.