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.
PHP:
-
/**
-
* 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
-
*/
-
-
-
function encode_html($str, $type = ENC_TO_DEC)
-
{
-
$result = '';
-
switch($type) {
-
case ENC_TO_ENT:
-
for ($i = 127; $i <255; $i++) {
-
}
-
break;
-
case ENC_TO_DEC:
-
for ($i = 0; $i <strlen($str); $i++) {
-
$result .= ($cdec <= 127) ? $str{$i} : "&#{$cdec};";
-
}
-
break;
-
case ENC_TO_HEX:
-
for ($i = 0; $i <strlen($str); $i++) {
-
}
-
break;
-
case ENC_TO_URL:
-
break;
-
default:
-
$result = $str;
-
break;
-
}
-
return $result;
-
}




0 Responses to “String encoding for HTML”
Leave a Reply
You must login to post a comment.