Jump to content

Cleaning a STRING


dannybrazil

Recommended Posts

Hello,

 

I have a list of EMAILS which I want to clean.

ex.

[email protected];

Adriano Gomes Benício <[email protected]>;

"[email protected]" <[email protected]>;

 

I would like to CLEAN the list and get ONLY the email address like this:

 

[email protected],

[email protected]

 

It means removing    /names/ < / > / " / ;/ and add a COMA after each email.

 

I have this code, but it doesnt clean ALL

 

$patterns = array();
$patterns[0] = '/;/';


$replacements = array();
$replacements[2] = ',<br>';


echo preg_replace($patterns, $replacements, $string);

 

any help ?

Link to comment
https://forums.phpfreaks.com/topic/239802-cleaning-a-string/
Share on other sites

I have found this script

$text = ' [email protected] ';

function parseTextForEmail($text) {
$email = array();
$invalid_email = array();

$text = ereg_replace("[^A-Za-z._0-9@ ]"," ",$text);

$token = trim(strtok($text, " "));

while($token !== "") {

	if(strpos($token, "@") !== false) {

		$token = ereg_replace("[^A-Za-z._0-9@]","", $token);

		//checking to see if this is a valid email address
		if(is_valid_email($email) !== true) {
			$email[] = strtolower($token);
		}
		else {
			$invalid_email[] = strtolower($token);
		}
	}

	$token = trim(strtok(" "));
}

$email = array_unique($email);
$invalid_email = array_unique($invalid_email);

return array("valid_email"=>$email, "invalid_email" => $invalid_email);

}

function is_valid_email($email) {
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$",$email)) return true;
else return false;
}

var_dump(parseTextForEmail($text));

 

How to I get ONLY the emails to be printed:

[email protected],

[email protected],

[email protected]

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/239802-cleaning-a-string/#findComment-1231815
Share on other sites

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.