note 56150 added to function.htmlentities
| From: | php-general at lists dot php dot net | Date: | Thu, 25 Aug 2005 04:04:06 +0000 |
| Subject: | note 56150 added to function.htmlentities | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94161@lists.php.net to get a copy of this message | ||
Regarding the post made by tmp1000 I have made modifications that will blank out all characters that
are in Windows-1252 and not in ISO 8859-1. My problem was when pulling xml data from a database
that users had submitted the text. The xml parser would fail on these particular characters and
this function will fix that problem.
function xmlentities($string, $quote_style=ENT_QUOTES)
{
static $trans;
if (!isset($trans)) {
$trans = get_html_translation_table(HTML_ENTITIES, $quote_style);
foreach ($trans as $key => $value)
$trans[$key] = '&#'.ord($key).';';
// dont translate the '&' in case it is part of &xxx;
$trans[chr(38)] = '&';
// the following are all the invalid characters from Windows-1252 to ISO-8859-1
$trans[chr(9)] = '';
$trans[chr(10)] = '';
$trans[chr(11)] = '';
$trans[chr(12)] = '';
$trans[chr(130)] = '';
$trans[chr(131)] = '';
$trans[chr(132)] = '';
$trans[chr(133)] = '';
$trans[chr(134)] = '';
$trans[chr(135)] = '';
$trans[chr(136)] = '';
$trans[chr(137)] = '';
$trans[chr(138)] = '';
$trans[chr(139)] = '';
$trans[chr(140)] = '';
$trans[chr(141)] = '';
$trans[chr(142)] = '';
$trans[chr(143)] = '';
$trans[chr(144)] = '';
$trans[chr(145)] = '';
$trans[chr(146)] = '';
$trans[chr(147)] = '';
$trans[chr(148)] = '';
$trans[chr(149)] = '';
$trans[chr(150)] = '';
$trans[chr(151)] = '';
$trans[chr(152)] = '';
$trans[chr(153)] = '';
$trans[chr(154)] = '';
$trans[chr(155)] = '';
$trans[chr(156)] = '';
$trans[chr(157)] = '';
$trans[chr(158)] = '';
$trans[chr(159)] = '';
}
// after the initial translation, _do_ map standalone '&' into
'&'
return
preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&" ,
strtr($string, $trans));
}
----
Manual Page -- http://www.php.net/manual/en/function.htmlentities.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56150
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56150&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56150&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56150&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56150&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56150&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56150&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56150&report=yes
Search -- http://master.php.net/manage/user-notes.php