Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-09-2006, 11:09 PM   PM User | #1
GO ILLINI
Regular Coder

 
GO ILLINI's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 634
Thanks: 0
Thanked 7 Times in 7 Posts
GO ILLINI is an unknown quantity at this point
variable from xml

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
__________________
Why not thank me?

http://adamsworld.name
GO ILLINI is offline   Reply With Quote
Old 06-10-2006, 04:56 AM   PM User | #2
lavinpj1
Regular Coder

 
Join Date: Sep 2005
Posts: 394
Thanks: 1
Thanked 0 Times in 0 Posts
lavinpj1 is an unknown quantity at this point
I did this recently and just used a bit of simple regex...

PHP Code:
<?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~
lavinpj1 is offline   Reply With Quote
Old 06-10-2006, 05:54 AM   PM User | #3
GO ILLINI
Regular Coder

 
GO ILLINI's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 634
Thanks: 0
Thanked 7 Times in 7 Posts
GO ILLINI is an unknown quantity at this point
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
__________________
Why not thank me?

http://adamsworld.name
GO ILLINI is offline   Reply With Quote
Old 06-10-2006, 01:39 PM   PM User | #4
lavinpj1
Regular Coder

 
Join Date: Sep 2005
Posts: 394
Thanks: 1
Thanked 0 Times in 0 Posts
lavinpj1 is an unknown quantity at this point
Yes...

Code:
<?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...

Code:
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~
lavinpj1 is offline   Reply With Quote
Old 06-11-2006, 07:58 AM   PM User | #5
GO ILLINI
Regular Coder

 
GO ILLINI's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 634
Thanks: 0
Thanked 7 Times in 7 Posts
GO ILLINI is an unknown quantity at this point
cool man thanks works like a charm.
thanks


The one and only,
ILLINI
__________________
Why not thank me?

http://adamsworld.name
GO ILLINI is offline   Reply With Quote
Old 06-11-2006, 12:12 PM   PM User | #6
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,530
Thanks: 15
Thanked 128 Times in 121 Posts
chump2877 is on a distinguished road
quick question...in your regex pattern, what is the red part doing exactly:

$pattern = '@(<description>).*?(?=</description>)@';
__________________
Regards, R.J.
chump2877 is online now   Reply With Quote
Old 06-11-2006, 07:51 PM   PM User | #7
lavinpj1
Regular Coder

 
Join Date: Sep 2005
Posts: 394
Thanks: 1
Thanked 0 Times in 0 Posts
lavinpj1 is an unknown quantity at this point
?= 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~
lavinpj1 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:27 PM.


Advertisement
Log in to turn off these ads.