Encode string to character codes

I want to encode the email into the appropriate 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) (added spaces displayed by the browser):

&#97 ;&#98 ;&#99 ;&#64 ;&#97 ;&#98 ;&#99 ;&#46 ;&#99 ;&#111 ;&#109 ;

      

for humans to read (1) and web robots to read (2)

There must be a simple function or way to do it, but can't find it.

+3


source to share


3 answers


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

+2


source


You can try this:



<?php
$s = "abc@abc.com";
$obj = array_map(function($x){return "&#". strval(ord($x)) . ";";},str_split($s));
echo implode($obj);
?>

      

+4


source


Here's a neat function I wrote to do something like this - not sure if it's exactly what you're looking for, but uses a php function ord

to take every character in a string and output its ascii equivalent:

$testString = "I hate – Character – 150" ;

function printAscii($string){      
    for ($n=0;$n<strlen($string);$n++){ 

       echo "<pre>";
       echo ord($string[$n]);
       echo " ---->";
       echo $string[$n];
       echo "</pre>"; 
    }          
} 

$test = printAscii($testString);

      

+1


source







All Articles