请百分之百放心机器人不会来破解。
PHP代码
- /**
- * Encoded Mailto Link
- *
- * Create a spam-protected mailto link written in Javascript
- *
- * @access public
- * @param string the email address
- * @param string the link title
- * @param mixed any attributes
- * @return string
- */
- if ( ! function_exists('safe_mailto'))
- {
- function safe_mailto($email, $title = '', $attributes = '')
- {
- $title = (string) $title;
- if ($title == "")
- {
- $title = $email;
- }
- for ($i = 0; $i < 16; $i++)
- {
- $x[] = substr('<a href="mailto:', $i, 1);
- }
- for ($i = 0; $i < strlen($email); $i++)
- {
- $x[] = "|".ord(substr($email, $i, 1));
- }
- $x[] = '"';
- if ($attributes != '')
- {
- if (is_array($attributes))
- {
- foreach ($attributes as $key => $val)
- {
- $x[] = ' '.$key.'="';
- for ($i = 0; $i < strlen($val); $i++)
- {
- $x[] = "|".ord(substr($val, $i, 1));
- }
- $x[] = '"';
- }
- }
- else
- {
- for ($i = 0; $i < strlen($attributes); $i++)
- {
- $x[] = substr($attributes, $i, 1);
- }
- }
- }
- $x[] = '>';
- $temp = array();
- for ($i = 0; $i < strlen($title); $i++)
- {
- $ordinal = ord($title[$i]);
- if ($ordinal < 128)
- {
- $x[] = "|".$ordinal;
- }
- else
- {
- if (count($temp) == 0)
- {
- $count = ($ordinal < 224) ? 2 : 3;
- }
- $temp[] = $ordinal;
- if (count($temp) == $count)
- {
- $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
- $x[] = "|".$number;
- $count = 1;
- $temp = array();
- }
- }
- }
- $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
- $x = array_reverse($x);
- ob_start();
- ?><script type="text/javascript">
- //<![CDATA[
- var l=new Array();
- <?php
- $i = 0;
- foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
- for (var i = l.length-1; i >= 0; i=i-1){
- if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
- else document.write(unescape(l[i]));}
- //]]>
- </script><?php
- $buffer = ob_get_contents();
- ob_end_clean();
- return $buffer;
- }
- }
使用:
PHP代码
- <?php
- echo safe_mailto('yourname@yourdomain.com');
会生成类似这样一段代码:
JavaScript代码
- <script type="text/javascript">
- //<![CDATA[
- var l=new Array();
- l[0]='>';l[1]='a';l[2]='/';l[3]='<';l[4]='|109';l[5]='|111';l[6]='|99';l[7]='|46';l[8]='|105';l[9]='|119';l[10]='|110';l[11]='|111';l[12]='|105';l[13]='|115';l[14]='|64';l[15]='|101';l[16]='|99';l[17]='|105';l[18]='|118';l[19]='|114';l[20]='|101';l[21]='|115';l[22]='>';l[23]='"';l[24]='|109';l[25]='|111';l[26]='|99';l[27]='|46';l[28]='|105';l[29]='|119';l[30]='|110';l[31]='|111';l[32]='|105';l[33]='|115';l[34]='|64';l[35]='|101';l[36]='|99';l[37]='|105';l[38]='|118';l[39]='|114';l[40]='|101';l[41]='|115';l[42]=':';l[43]='o';l[44]='t';l[45]='l';l[46]='i';l[47]='a';l[48]='m';l[49]='"';l[50]='=';l[51]='f';l[52]='e';l[53]='r';l[54]='h';l[55]=' ';l[56]='a';l[57]='<';
- for (var i = l.length-1; i >= 0; i=i-1){
- if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
- else document.write(unescape(l[i]));}
- //]]>
- </script>
显示效果如下:service@sionwi.com