String encoding for HTML

I find this a handy function to encompass a number of different string encoding routines. It allows you to do entity encoding (ÿ), hex (á), html (&aecute;), or url encoding (%E1).

It uses a number of defined constants for convenience.

/**
* Encode a string for HTML usage
*
* This method will encode characters (such as accented characters, &,
* >, and so on) to either their equivelant entities in decimal (ÿ),
* hex (á) or html (&aecute;). It can also be used to convert to
* url encode, %E1.
*
* Encoding will only happen to characters greater than ‘~’ (chr(127)).
*
* If an incorrect encoding type is passed it will simply return the
* string in the same state.
*
* @param string $str The string to encode
* @param integer $type The type of encoding to use
* @return string
* @access public
*/

define(‘ENC_TO_DEC’, 0);
define(‘ENC_TO_HEX’, 1);
define(‘ENC_TO_ENT’, 2);
define(‘ENC_TO_URL’, 3);

function encode_html($str, $type = ENC_TO_DEC)
{
$result = ”;
switch($type) {
case ENC_TO_ENT:
$fixed = htmlentities($str, ENT_QUOTES);
$trans = array();
for ($i = 127; $i < 255; $i++) { $trans[chr($i)] = "&#{$i};"; } $result = strtr($fixed, $trans); break; case ENC_TO_DEC: for ($i = 0; $i < strlen($str); $i++) { $cdec = ord($str{$i}); $result .= ($cdec <= 127) ? $str{$i} : "&#{$cdec};"; } break; case ENC_TO_HEX: for ($i = 0; $i < strlen($str); $i++) { $cdec = ord($str{$i}); $result .= ($cdec <= 127) ? $str{$i} : '&#x' . strtoupper(dechex($cdec)) . ';'; } break; case ENC_TO_URL: $result = urlencode($str); break; default: $result = $str; break; } return $result; }[/php]

Did you like this? Share it:

Leave a Reply