Encode a string into character codes

character, php, string

Solution

function encode_everything($string){ 
    $encoded = ""; 
    for ($n=0;$n<strlen($string);$n++){ 
        $check = htmlentities($string[$n],ENT_QUOTES); 
       $string[$n] == $check ? $encoded .= "&#".ord($string[$n]).";" : $encoded .= $check; 
    } 
    return $encoded; 
} 

Found at:

http://php.net/manual/en/function.htmlentities.php

Problem

I want to encode an email address into its corresponding character codes, so when it is printed the char codes are interpreted by the browser, but the robots get the encoded string instead of the interpreted one. For example (1): ``` abc@abc.com ``` should be sent to the browser as (2) (whitespaces added so the browser shows it): ``` &#97 ;&#98 ;&#99 ;&#64 ;&#97 ;&#98 ;&#99 ;&#46 ;&#99 ;&#111 ;&#109 ; ``` so the human reads (1) and web robots read(2) There should be an easy function or way to do this, but cannot find one.

Original source