Jump to content

remove a ; from emails in textarea


brown2005

Recommended Posts

<?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

Link to comment
Share on other sites

<?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
Share on other sites

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?  :confused:

 

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']);

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.