note 93432 added to function.trim
| From: | holek dot n at gmail dot com | Date: | Wed, 09 Sep 2009 22:59:02 +0000 |
| Subject: | note 93432 added to function.trim | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160372@lists.php.net to get a copy of this message | ||
Admendum to this note:
<?php
// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
$clean = trim($binary, "\x00..\x1F");
?>
I had a big time with my WWW parser, which produced XML errors because of illegal characters in the
outside files. So I decided to wipe them out.
If you want to remove all illegal SGML characters (ASCII coces 0 to 31 inclusive and 127 to 159
inclusive) from the whole string, (remember that trim doesn't work inside a string, just on the
borders) here's how to do that:
<?php
$title = "Autobus zamiast \x1EHetmana\x1D";
$replaceArray = array(array(), array()); // this is a replace array for illegal SGML characters;
for ($i=0; $i<32; $i++) // produces a correct XML output
{
$replaceArray[0][] = chr($i);
$replaceArray[1][] = "";
}
for ($i=127; $i<160; $i++)
{
$replaceArray[0][] = chr($i);
$replaceArray[1][] = "";
}
$title = str_replace($replaceArray[0], $replaceArray[1], $title); // get rid of illegal SGML chars
echo $title; // prints out "Autobus zamiast Hetmana"
?>
Now $title can be printed into an XML/XHTML file without worries.
----
Server IP: 82.177.164.100
Probable Submitter: 78.8.2.103
----
Manual Page -- http://www.php.net/manual/en/function.trim.php
Edit -- https://master.php.net/note/edit/93432
Del: integrated -- https://master.php.net/note/delete/93432/integrated
Del: useless -- https://master.php.net/note/delete/93432/useless
Del: bad code -- https://master.php.net/note/delete/93432/bad+code
Del: spam -- https://master.php.net/note/delete/93432/spam
Del: non-english -- https://master.php.net/note/delete/93432/non-english
Del: in docs -- https://master.php.net/note/delete/93432/in+docs
Del: other reasons-- https://master.php.net/note/delete/93432
Reject -- https://master.php.net/note/reject/93432
Search -- https://master.php.net/manage/user-notes.php