note 70049 added to ref.xml
| From: | hutch at midwales dot com | Date: | Sun, 01 Oct 2006 16:26:43 +0000 |
| Subject: | note 70049 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117904@lists.php.net to get a copy of this message | ||
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
----
Server IP: 66.163.161.117
Probable Submitter: 89.145.198.59
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- https://master.php.net/note/edit/70049
Del: integrated -- https://master.php.net/note/delete/70049/integrated
Del: useless -- https://master.php.net/note/delete/70049/useless
Del: bad code -- https://master.php.net/note/delete/70049/bad+code
Del: spam -- https://master.php.net/note/delete/70049/spam
Del: non-english -- https://master.php.net/note/delete/70049/non-english
Del: in docs -- https://master.php.net/note/delete/70049/in+docs
Del: other reasons-- https://master.php.net/note/delete/70049
Reject -- https://master.php.net/note/reject/70049
Search -- https://master.php.net/manage/user-notes.php