tags:

views:

81

answers:

6

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.

I would like to take an email address eg [email protected] and break it to: $variable1 = "info"; $variable2 = "thiscompany.com";

All this done in PHP.

Regards, JB

+2  A: 
$parts = explode("@", $email_address);

Assuming that $email_address = '[email protected]' then $parts[0] == 'info' and $parts[1] == 'thiscompany.com'

thetaiko
+1  A: 

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;

}
zaf
Thank you all for your insight, this is what I wanted to do:<script language="Javascript" type="text/javascript"><!-- // hide from old browsers//variablesvar part_1 = "info"; var part_2 = "mycompany.com"; //outputdocument.write('<a href=\"mailto:' + part_1 + '@' + part_2 + '\">'); document.write(part_1 + '@' + part_2);document.write('</a>'); // --></script> I'm grateful to zaf, your solution does it perfectly.Thanks again,JB
Jay Bee
+2  A: 

You can use explode:

$email = '[email protected]';

$arr = explode('@',$email);

$part1 = $arr[0]; // info
$part2 = $arr[1]; // thiscompany.com
codaddict
+2  A: 
$email = "[email protected]";
$parts = explode("@", $email);
James Burgess
+5  A: 
list($variable1, $variable2) = explode('@','[email protected]');
Brant
+1 for using list() :)
James Burgess
@James - Thanks
Brant
A: 

How about a function for parsing strings according to a given format: sscanf. For example:

sscanf('[email protected]', '%[^@]@%s', $variable1, $variable2);
salathe