brown2005 Posted October 29, 2009 Share Posted October 29, 2009 <?php session_start(); $database_host = "localhost"; $database_username = ""; $database_password = ""; $database_name = ""; $connection = mysql_connect($database_host, $database_username, $database_password) or die(mysql_error()); $db = mysql_select_db($database_name, $connection); if(!$_POST) { echo "<FORM METHOD=POST ACTION=$PHP_SELF>"; echo "<TEXTAREA NAME=my_textarea COLS=30 ROWS=5></TEXTAREA><BR>"; echo "<INPUT TYPE=SUBMIT VALUE=Submit>"; echo "</FORM>"; } else { $entries = explode( "\n", $_POST[my_textarea]); $x=0; foreach($entries AS $entry) { if($entry!=="") { $register_sql = "INSERT INTO emails(emails_email) VALUES ('$entry')"; $register_row = mysql_query($register_sql); echo "$entry added to database<br>"; $x++; } } } ?> i have the above code, where you enter emails in a textarea like email1@hotmail.com email2@hotmail.com and then it will put them into my database. right now what i want to do is adapt the above code to do the same when you put emails in the textarea like.. email1@hotmail.com; email2@hotmail.com thanks in advance Quote Link to comment Share on other sites More sharing options...
seanlim Posted October 29, 2009 Share Posted October 29, 2009 use str_replace to find and replace any ";" with "\n". Continue to use your explode function to break the email addresses apart on "\n". Then, use trim() to remove any unwanted white-spaces for each email address. Quote Link to comment Share on other sites More sharing options...
ironhamster88 Posted October 29, 2009 Share Posted October 29, 2009 <?php if(strpos($_POST['email'], ';')) { // break string into array by semicolons $arr = explode(';', $_POST['email']); // loop through the array of emails foreach($arr as $email) { // remove any spaces in the email address (if the user has used ; followed by a space to separate emails) $email = trim($email); // insert $email into database } } else { // normal insert functions for singular emails } ?> Quote Link to comment Share on other sites More sharing options...
seanlim Posted October 29, 2009 Share Posted October 29, 2009 That would work only for a email addresses separated by ";", and not "\n". I believe the OP was looking for a solution to do both at the same time? email1@hotmail.com;email2@hotmail.com email3@hotmail.com either way, just add this line in ironhamster's code for both to work: $_POST['email'] = str_replace(";", "\n", $_POST['email']); Quote Link to comment Share on other sites More sharing options...
brown2005 Posted October 29, 2009 Author Share Posted October 29, 2009 like this... { $entries = explode( "\n", $_POST[my_textarea]); $entries = str_replace(";", "\n", $entries); $x=0; foreach($entries AS $entry) { if($entry!=="") { $register_sql = "INSERT INTO emails(emails_email) VALUES ('$entry')"; $register_row = mysql_query($register_sql); echo "$entry added to database<br>"; $x++; } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.