[thelist] php regexp help (or some other solution maybe..)

Kelly Hallman khallman at wrack.org
Tue Dec 31 14:27:01 CST 2002

On Tue, 31 Dec 2002, Tom Dell'Aringa wrote:
> At this point I don't have a file. Per your question below, what I
> have is a response from a proprietary DB from an XML query to it. I
> was dumping this response into a file (dump.xml), but the header info
> in the file was of course causing problems (its not a valid XML file
> with the header junk in there).

The reason I was asking about the temporary files is because if you are
simply trying to parse the data you can do it without a single fopen()..

(Assuming, your parser is all set up) Consider this:

$fname = "http://domain.com/pathtoxml/dump.xml";

list($head,$body) =
    preg_split("/(?=<\?xml)/m", join("",file($fname)), 2);


It loads the XML file (from a URL, or it could be a local file), strips
the header, and feeds the XML data to the parser, without any local file
operations.  If you were working with a 20mb XML file, you might want to
do some benchmarking, but with XML of several hundred Kb, it's probably
six of one half a dozen of the other...

If you needed the temporary files for some other purpose, you might still
approach it with this code, instead of lots of looping over the data..

> I do have a recent version of PHP too, 4.1.2.

Unfortunately, file_get_contents() is apparently PHP 4.3+...
$x = join("",file($fname)) is an easy workaround..

Kelly Hallman

More information about the thelist mailing list