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

Kelly Hallman khallman at wrack.org
Tue Dec 31 13:36:01 CST 2002


On Tue, 31 Dec 2002, Tom Dell'Aringa wrote:
> --- Kelly Hallman <khallman at wrack.org> wrote:
> > This regex uses a positive lookahead:
> > list($head,$body) = preg_split("/(?=<\?xml)/m",$inputdata,2);
>
> Sorry if this is a stupid question, but how do I use it (i really
> have no understanding of regex at all) within this code block:

Well, using the above line of code, $inputdata is your entire file and
afterward $body contains the data you want.  Hence, yesterday's code:

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

Which rolls the file loading into the same line.  (If you have a very
recent version of PHP you could use file_get_contents() instead of the
join(file()) combination to get the file into a single string.)

> > You could omit the limit argument to preg_split, if you wanted to
> > split at every XML PI, but this example just assumes two parts.
>
> As if I knew what that meant!

I just refreshed myself on XML in the last weeks while developing our
Flash holiday card.  That is how I know that <?xml is a PI, just as <?php
is a PI.  Of course, I don't remember what PI stands for... :)

If you meant the limit argument, it just specifies the maximum number of
results from the split, see the preg_split() manual page.  For the code
above it means we only want two results.  Any further pattern <?xml would
not match for purposes of the split.  Which leaves you with the header and
the body, for easy assignment to a list of two variables.

Then you can run $body through xml_parse() in one shot, without a loop.

> > * Let's see if that improves response :)
>
> Actually the reason I didn't try your solution earlier was because I
> think I had the other one running already..would your solution still
> have the same effect as the block of data that you mentioned?

Well, I am not sure why you are doing all the file reading and writing, so
I am not sure how my solution fits into your code exactly.  Is it just as
temporary files, to prepare the data for processing in the same request?
(i.e. is this the result of a visitor's web hit?)  If so, consider the
in-memory approach outlined here, which is probably not so inefficient
when compared against file operations.

For fun, (I was going to post this to the list but I wasn't sure if it
actually applied) our holiday card is here: http://wrackmedia.com/holiday/
The songs and list of songs are stored in XML format and grabbed
dynamically from the server by the Flash movie.

--
Kelly Hallman
http://wrack.org/




More information about the thelist mailing list