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

 

[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

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

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:

 

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

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++;
        
		}
    
	}

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.