note 61204 added to function.htmlentities
| From: | timburgan<at>gmail" com | Date: | Sat, 28 Jan 2006 03:12:31 +0000 |
| Subject: | note 61204 added to function.htmlentities | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102596@lists.php.net to get a copy of this message | ||
To encode characters within a string to their HTML character code (in the format &#xx;), use the
following function encode_entities().
A list of HTML character codes can be found at: http://www.ascii.cl/htmlcodes.htm
Note: To dencode a string that contains HTML character codes (in the format &#xx;), use the
function unicode_decode(), listed in the comments of http://php.net/manual/function.html-entity-decode.php
<?php
/**
* Encode each char in a string to its HTML code.
* Characters encoded by this function can be decoded
* by unicode_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
* @see http://php.net/manual/function.html-entity-decode.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 space chars are encoded.
* @return string Returns a string of encoded chars
*/
function unicode_entities($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);
}
?>
Usage:
<?php
$string = '<a href="http://timburgan.com"
rel="external" title="Go to timburgan.com">Tim Burgan</a>';
echo unicode_entities($string);
?>
Output:
<a href="http://timburgan.com"
rel="external" title="Go to
timburgan.com">Tim Burgan</a>
----
Server IP: 203.220.0.235
Probable Submitter: 203.26.206.129 (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+61204
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61204&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61204&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61204&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61204&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61204&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61204&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61204&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61204&report=yes
Search -- http://master.php.net/manage/user-notes.php