note 61383 deleted from function.htmlentities by nlopess
| From: | nlopess@php.net | Date: | Sat, 30 Dec 2006 18:50:22 +0000 |
| Subject: | note 61383 deleted from function.htmlentities by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-121116@lists.php.net to get a copy of this message | ||
Note Submitter: timburgan at gmail dot com
----
chars_encode() will, by default, convert ALL non-alpha-numeric and non-space characters in a string
to it's ASCII HTML character code (i.e. + to +).
chars_decode() will decode a string encoded by chars_encode().
<?php
/**
* Encode each char in a string to its HTML ASCII code.
* Characters encoded by this function can be decoded
* by chars_decode().
*
* This function will work with PHP >= 3.0.9, 4, 5.
*
* @author Tim Burgan <timburgan@gmail.com>
* @version 1.0.0
* @link http://timburgan.com/
* @link http://php.net/manual/function.htmlentities.php
* @param string $string String to encode
* @param bool $encodeAll Optional. Default is false. If true all chars are encoded, if
false, only non-alpha-numeric and non-space chars are encoded.
* @return string Returns a string of encoded chars
*/
function chars_encode($string, $encodeAll = false)
{
// declare variables
$chars = array();
$ent = null;
// split string into array
$chars = preg_split("//", $string, -1, PREG_SPLIT_NO_EMPTY);
// encode each character
for ( $i = 0; $i < count($chars); $i++ )
{
if ( preg_match("/^(\w| )$/",$chars[$i]) && $encodeAll == false )
$ent[$i] = $chars[$i];
else
$ent[$i] = "&#" . ord($chars[$i]) . ";";
}
if ( sizeof($ent) < 1)
return "";
return implode("",$ent);
}
/**
* Decode each char in a string from its HTML ASCII code.
* Characters denoded by this function were encoded
* by chars_encode().
*
* This function will work with PHP >= 3.0.9, 4, 5.
*
* @author Tim Burgan <timburgan@gmail.com>
* @version 1.0.0
* @link http://timburgan.com/
* @link http://php.net/manual/function.html-entity-decode.php
* @param string $string String to decode
* @return string Returns a string of decoded chars
*/
function chars_decode($string)
{
// declare variables
$tok = 0;
$cur = 0;
$chars = null;
// move through the string until the end is reached
while ( $cur < strlen($string) )
{
// find the next token
$tok = strpos($string, "&#", $cur);
// if no more tokens exist, move pointer to end of string
if ( $tok === false )
$tok = strlen($string);
// if the current char is alpha-numeric or a space
if ( preg_match("/^(\w| )$/",substr($string, $cur, 1)) )
{
$chars .= substr($string, $cur, $tok - $cur);
}
// the current char must be the start of a token
else
{
$cur += 2;
$tok = strpos($string, ';', $cur);
$chars .= chr(substr($string, $cur, $tok - $cur));
$tok++;
}
// move the current pointer to the next token
$cur = $tok;
}
return $chars;
}
/* Example usage
***********************************************/
$string = '<a href="http://timburgan.com"
rel="external" title="Go to timburgan.com">Tim Burgan</a>';
$encoded = chars_encode($string);
$decoded = chars_decode($encoded);
echo <<<HEREDOCS
<h4>Original String Output</h4>
{$string}
<hr/>
<h4>Encoded String Output</h4>
{$encoded}
<hr/>
<h4>Decoded String Output</h4>
{$decoded}
HEREDOCS;
?>