note 93432 deleted from function.trim by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 12:30:48 +0000 |
| Subject: | note 93432 deleted from function.trim by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189671@lists.php.net to get a copy of this message | ||
Note Submitter: holek dot n at gmail dot com
----
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.