note 70049 deleted from ref.xml by danbrown
| From: | danbrown@php.net | Date: | Wed, 03 Mar 2010 18:53:32 +0000 |
| Subject: | note 70049 deleted from ref.xml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166613@lists.php.net to get a copy of this message | ||
Note Submitter: hutch at midwales dot com
----
First off, I'd like thank all and sundry for providing this excellent resource, it has been
very helpful in getting my head around xml parsing.
I was recently handed the task of collecting a variety of xml streams, from many different sources
and of widely varying quality.
If have found that the following function helped parsing the input by cleaning it up. It removes all
leading and trailing whitespace and removes carriage returns and linefeeds.
Using this function before using xml_parser_create() has helped reduce a number of otherwise
unexplainable anomalies, such as arbitrary cutoff of data or the data being divided into two,
requiring concatenation. Data longer than 1024 characters still has to be concatenated, but I can
live with that.
<?php
// remove whitespace and linefeeds and returns the name of a temporary file
// takes the name of an existing file as a parameter
function cleanxmlfile($file, $tmpdir="/tmp", $prefix="xxx_") {
$tmp = file_get_contents ($file);
$tmp = preg_replace("/^\s+/m","",$tmp);
$tmp = preg_replace("/\s+$/m","",$tmp);
$tmp = preg_replace("/\r/","",$tmp);
$tmp = preg_replace("/\n/","",$tmp);
$tmpfname = tempnam($tmpdir, $prefix);
$handle = fopen($tmpfname, "w");
fwrite($handle, "$tmp");
fclose($handle);
return($tmpfname);
}
?>
HTH