note 58314 added to function.ord
| From: | MatthewFlint at osu1 dot php dot net | Date: | Mon, 31 Oct 2005 10:59:43 +0000 |
| Subject: | note 58314 added to function.ord | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97873@lists.php.net to get a copy of this message | ||
I wrote the following function to clean illegal characters from input strings.
(Background: I have a php-based news website. People were writing articles in MS Word, then
copy-and-pasting the text into the website. Word uses non-standard characters for opening and
closing quotes and double-quotes, and for "..." - and this was resulting in articles on
the website that failed XHTML validation)
<?php
function clean_string_input($input)
{
$interim = strip_tags($input);
if(get_magic_quotes_gpc())
{
$interim=stripslashes($interim);
}
// now check for pure ASCII input
// special characters that might appear here:
// 96: opening single quote (not strictly illegal, but substitute anyway)
// 145: opening single quote
// 146: closing single quote
// 147: opening double quote
// 148: closing double quote
// 133: ellipsis (...)
// 163: pound sign (this is safe, so no substitution required)
// these can be substituted for safe equivalents
$result = '';
for ($i=0; $i<strlen($interim); $i++)
{
$char = $interim{$i};
$asciivalue = ord($char);
if ($asciivalue == 96)
{
$result .= '\\'';
}
else if (($asciivalue > 31 && $asciivalue < 127) ||
($asciivalue == 163) || // pound sign
($asciivalue == 10) || // lf
($asciivalue == 13)) // cr
{
// it's already safe ASCII
$result .= $char;
}
else if ($asciivalue == 145) // opening single quote
{
$result .= '\\'';
}
else if ($asciivalue == 146) // closing single quote
{
$result .= '\\'';
}
else if ($asciivalue == 147) // opening double quote
{
$result .= '"';
}
else if ($asciivalue == 148) // closing double quote
{
$result .= '"';
}
else if ($asciivalue == 133) // ellipsis
{
$result .= '...';
}
}
return $result;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.ord.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58314
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58314&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58314&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58314&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58314&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58314&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58314&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58314&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58314&report=yes
Search -- http://master.php.net/manage/user-notes.php