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 [email protected] [email protected] 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.. [email protected]; [email protected] thanks in advance Link to comment https://forums.phpfreaks.com/topic/179484-remove-a-from-emails-in-textarea/ 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. Link to comment https://forums.phpfreaks.com/topic/179484-remove-a-from-emails-in-textarea/#findComment-946973 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 } ?> Link to comment https://forums.phpfreaks.com/topic/179484-remove-a-from-emails-in-textarea/#findComment-946985 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? [email protected];[email protected] [email protected] either way, just add this line in ironhamster's code for both to work: $_POST['email'] = str_replace(";", "\n", $_POST['email']); Link to comment https://forums.phpfreaks.com/topic/179484-remove-a-from-emails-in-textarea/#findComment-946993 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++; } } } Link to comment https://forums.phpfreaks.com/topic/179484-remove-a-from-emails-in-textarea/#findComment-947077 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.