htmlentities for alphanumeric characters

I want to change ALL characters to HTML entities, INCLUDING alphanumeric characters.  Is there a way to do this built into php?
earthman100Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
You can use ASCII codes like:

&#65   for 'A'
&#49   for '1'

Have a look to:
http://www.bbsinc.com/iso8859.html
0
earthman100Author Commented:
I know, I want to convert the letters to the Ascii equivalents automatically with php.  Any idea how to do that?
0
Diablo84Commented:
Example:

<?php
function allentities($string) {
 $chars = array(
  "a" => "&#97;",
  "b" => "&#98;",
  "c" => "&#99;",
  "d" => "&#100;",
  "e" => "&#101;",
  "f" => "&#102;",
  "g" => "&#103;",
  "h" => "&#104;",
  "i" => "&#105;",
  "j" => "&#106;",
  "k" => "&#107;",
  "l" => "&#108;",
  "m" => "&#109;",
  "n" => "&#110;",
  "o" => "&#111;",
  "p" => "&#112;",
  "q" => "&#113;",
  "r" => "&#114;",
  "s" => "&#115;",
  "t" => "&#116;",
  "u" => "&#117;",
  "v" => "&#118;",
  "w" => "&#119;",
  "x" => "&#120;",
  "y" => "&#121;",
  "z" => "&#122;"
 );
 return str_replace(array_keys($chars),$chars,htmlentities($string));
}

echo allentities("hello");
?>
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Diablo84Commented:
I have only coded the above for the letters a-z, you would need to add A-Z and 0-9 but this shows how to do it.

Please refer to http://www.asciitable.com/ for the html codes you need for each entity.
0
Jaime OlivaresSoftware ArchitectCommented:
You can use the ord() function. Something like:

$test = "My test string 1234";

$result = "";
for ($i=0; $i<strlen($test); $i++) {
  $a = ord(substr($test,$i,1));

  $result .= "&#".$a.";";
}

Have not tested, please verify.

Good luck,
Jaime.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
earthman100Author Commented:
Thanks all.  Jaime's code is more efficient, even thoughDiablo's would work (although if I coded it to work for 0-9 then it would do wierd things with the 'htmlentities' function if any special characters were in the string)

Much appreciated.

I have now put the two together to create a javascript/php double encoding function to protect emails (especially ones derived from a database) from harvesting:

<?
######### BREAK EMAIL INTO BITS ###########
$email = "youremail@yourdomain.com";
$atsign = strpos($email,"@");
$emailname = substr($email,0,$atsign);
$emaildomain = substr($email,$atsign+1);
$emaildomain = "Re: Your subject line here";

########JAVSCRIPT WRITE TO DOC  ###########
echo '<script language="JavaScript" type="text/javascript"><!--
var name = "'.allentities($emailname).'";
var domain = "'.allentities($emaildomain).'";
document.write(\'<a href="mailto:\' + name + \'@\' + domain + \'?subject='.$emailsubject.'" style="text-decoration:underline;">\' + name + \'@\' + domain + \'</a>\');
// --></script>';

####### PHP ASCII ENCODING FUNCTION  #########
function allentities($string) {
$result = "";
for ($i=0; $i<strlen($string); $i++)
      {
    $a = ord(substr($string,$i,1));
    $result .= "&#".$a.";";
      }
return $result;
}
?>
0
earthman100Author Commented:
oops, change the second instance of $emaildomain to $emailsubject and it should all work.  cheers!
0
earthman100Author Commented:
or better yet:

<?
echo encodeEmail($email, "RE: Your email subject here");

function encodeEmail($email, $emailsubject)
{
######### BREAK EMAIL INTO BITS ###########
$atsign = strpos($email,"@");
$emailname = substr($email,0,$atsign);
$emaildomain = substr($email,$atsign+1);

######## JAVSCRIPT WRITE TO DOC  ###########
return '<script language="JavaScript" type="text/javascript"><!--
var name = "'.allentities($emailname).'";
var domain = "'.allentities($emaildomain).'";
document.write(\'<a href="mailto:\' + name + \'@\' + domain + \'?subject='.$emailsubject.'" style="text-decoration:underline;">\' + name + \'@\' + domain + \'</a>\');
// --></script>';
}

######## ENCODE TO ASCII  ###########
function allentities($string) {
$result = "";
for ($i=0; $i<strlen($string); $i++)
      {
    $a = ord(substr($string,$i,1));
    $result .= "&#".$a.";";
      }
return $result;
}

?>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.