You are here

function invisimail_ascii_encode in Invisimail 5

Same name and namespace in other branches
  1. 6 invisimail.module \invisimail_ascii_encode()

This function does the actual encoding.

Parameters

$string: A string which contains only an email addres to be encoded.

$js: Optional: A boolean which sets whether Javascript is used for encoding.

$link: Optional: A boolean which set whether the result includes a mailto link.

$text: Optional: The text to be used for the link.

Return value

An ascii encoded email address.

1 call to invisimail_ascii_encode()
invisimail_callback in ./invisimail.module

File

./invisimail.module, line 121
This module provides a filter that will search content for email addresses and replace them with their ascii equivalents before display. This is not a complete protection from spam harvesters, but it is some help.

Code

function invisimail_ascii_encode($string, $js = FALSE, $link = FALSE, $text = NULL) {
  if ($text == NULL) {
    $text = $string;
  }
  if ($js) {
    $output = "<script type='text/javascript'><!--\n    document.write('";
  }
  for ($i = 0; $i < strlen($string); $i++) {
    $char = substr($string, $i, 1);
    $encode .= '&#' . ord($char) . ';';
    if ($js) {
      if (in_array($char, array(
        '.',
        '@',
      ))) {

        // break strings after ats and dots
        $encode .= "'+'";
      }
    }
  }
  if ($link && !$js) {

    // ascii in this next line is "mailto:"
    $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;{$encode}\">{$encode}</a>";
  }
  elseif ($link && $js) {
    $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;'+'{$encode}'+'\">'+'{$encode}'+'</a>";
  }
  else {
    $output .= $encode;
  }
  if ($js) {
    $output .= "');\n    //-->\n    </script>";
  }
  return $output;
}