note 61383 added to function.htmlentities
| From: | timburgan at gmail dot com | Date: | Thu, 02 Feb 2006 02:39:27 +0000 |
| Subject: | note 61383 added to function.htmlentities | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102851@lists.php.net to get a copy of this message | ||
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;
?>
----
Server IP: 64.246.30.37
Probable Submitter: 203.26.206.130 (proxied: 203.122.231.20)
----
Manual Page -- http://www.php.net/manual/en/function.htmlentities.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61383
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61383&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61383&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61383&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61383&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61383&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61383&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61383&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61383&report=yes
Search -- http://master.php.net/manage/user-notes.php