...

View Full Version : variable from xml



GO ILLINI
06-10-2006, 12:09 AM
http://feeds.spreadfirefox.com/downloads/firefox.xml
Could someone please point me to a tutorial/ give me a script to find the value of description? I dont want to learn how to interact fully between xml and php yet just how to get this little thing done.

The one and only,
ILLINI

lavinpj1
06-10-2006, 05:56 AM
I did this recently and just used a bit of simple regex...


<?php
$content =
file_get_contents("http://feeds.spreadfirefox.com/downloads/firefox.xml");

$pattern = '@(<description>).*?(?=</description>)@';
preg_match_all($pattern, $content, $description);

$description = substr($description[0][0], 13);

echo 'The description is ' . $description;
?>


~Phil~

GO ILLINI
06-10-2006, 06:54 AM
Well that works great except for one problem... Is is possible to look for the second desctiption? The one with the number in it?
thanks

The one and only,
ILLINI

lavinpj1
06-10-2006, 02:39 PM
Yes...


<?php
$content =
file_get_contents("http://feeds.spreadfirefox.com/downloads/firefox.xml");

$pattern = '@(<description>).*?(?=</description>)@';
preg_match_all($pattern, $content, $matches);

$description = substr($matches[0][0], 13);
$description2 = substr($matches[0][1], 13);

echo 'The description is ' . $description . ' and the 2nd description
is ' . $description2;
?>


preg_match_all produces a 2d array. You can do something like...


echo '<pre>'; print_r($matches); echo '</pre>';

Just after the preg_match_all. This will give you an overview of the array created. The way 2,3,4,5 even 8934880203D arrays work, is you have arrays inside arrays inside arrays etc. This is really handy in some cases. You can see in my code, $matches[0][1]. This means item 0 of array 1 and item 1 of array 2. If you had a 5D array, you could do something like $matches[2][5][9][4][7].

Anywhooo, hope this helps

~Phil~

GO ILLINI
06-11-2006, 08:58 AM
cool man thanks works like a charm.
thanks


The one and only,
ILLINI

chump2877
06-11-2006, 01:12 PM
quick question...in your regex pattern, what is the red part doing exactly:

$pattern = '@(<description>).*?(?=</description>)@';

lavinpj1
06-11-2006, 08:51 PM
?= takes it before that point. i.e. If you had <description>hello hello hello</description> and didn't use a ?=, you'd get <description>hello hello hello</description> output. If you do use a ?=, you get <description>hello hello hello output. My regex isn't the best, and if someone would advise me on how to do a similar thing with the initial <description>, rather than having to substr it out, that would be very handy.

~Phil~



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum