[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

htmlentities for alphanumeric characters

Posted on 2004-11-10
8
Medium Priority
?
326 Views
Last Modified: 2013-12-03
I want to change ALL characters to HTML entities, INCLUDING alphanumeric characters.  Is there a way to do this built into php?
0
Comment
Question by:earthman100
  • 4
  • 2
  • 2
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12549454
You can use ASCII codes like:

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

Have a look to:
http://www.bbsinc.com/iso8859.html
0
 

Author Comment

by:earthman100
ID: 12549487
I know, I want to convert the letters to the Ascii equivalents automatically with php.  Any idea how to do that?
0
 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 400 total points
ID: 12549540
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Diablo84
ID: 12549544
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1600 total points
ID: 12549576
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
 

Author Comment

by:earthman100
ID: 12549707
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
 

Author Comment

by:earthman100
ID: 12549710
oops, change the second instance of $emaildomain to $emailsubject and it should all work.  cheers!
0
 

Author Comment

by:earthman100
ID: 12549774
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question