How can I break an email address into different parts in php?
Basically what I want to do is display an email using javascript to bring the parts together and form a complete email address that cannot be visible by email harvesters.
开发者_StackOverflow中文版I would like to take an email address eg info@thiscompany.com and break it to: $variable1 = "info"; $variable2 = "thiscompany.com";
All this done in PHP.
Regards, JB
list($variable1, $variable2) = explode('@','info@thiscompany.com');
$parts = explode("@", $email_address);
Assuming that $email_address = 'info@thiscompany.com'
then $parts[0] == 'info'
and $parts[1] == 'thiscompany.com'
You can use explode:
$email = 'info@thiscompany.com';
$arr = explode('@',$email);
$part1 = $arr[0]; // info
$part2 = $arr[1]; // thiscompany.com
$email = "info@thiscompany.com";
$parts = explode("@", $email);
Try this one before you roll your own (it does a lot more):
function hide_email($email)
{ $character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
$key = str_shuffle($character_set); $cipher_text = ''; $id = 'e'.rand(1,999999999);
for ($i=0;$i<strlen($email);$i+=1) $cipher_text.= $key[strpos($character_set,$email[$i])];
$script = 'var a="'.$key.'";var b=a.split("").sort().join("");var c="'.$cipher_text.'";var d="";';
$script.= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));';
$script.= 'document.getElementById("'.$id.'").innerHTML="<a href=\\"mailto:"+d+"\\">"+d+"</a>"';
$script = "eval(\"".str_replace(array("\\",'"'),array("\\\\",'\"'), $script)."\")";
$script = '<script type="text/javascript">/*<![CDATA[*/'.$script.'/*]]>*/</script>';
return '<span id="'.$id.'">[javascript protected email address]</span>'.$script;
}
How about a function for parsing strings according to a given format: sscanf. For example:
sscanf('info@thiscompany.com', '%[^@]@%s', $variable1, $variable2);
精彩评论