xpath php scrape help!

Soldato
Joined
7 Jan 2007
Posts
10,607
Location
Sussex, UK
This is my code:

PHP:
<?php
//a XML you want to retrieve
$xml = simplexml_load_file('url.com');

//Find the Second <description> tag and put contents into a string
$result = ($xml->xpath("//description[1]"));

// break $result using the br character as the delimiter
$number = explode('br', $result);
 
//removes every character & symbol except numbers
$number = preg_replace("/\D/", "", $number);

//print the number on it's own
echo $number;


This is a copy of the remote xml I am trying to parse:
PHP:
<rss version="2.0">
<channel>
<title>football results</title>
<link>
http://url.com
</link>
<description>
football results
</description>
<date>Tue, 24 Jan 2012 19:58:35 GMT</date>
<language>en</language>
<image>
<url>http://url.com/image.jpg</url>
<title>Football fixtures</title>
<link>
http://url.com
</link>
</image>
<lastBuildDate/>
<item>
<title>Football teams</title>
<link>
http://url.com
</link>
<description>
I WANT TO <br>SCRAPE THIS NUMBER</br> <br>IN HERE 5000000</br>
</description>
<pubDate>Tue, 02 Feb 2012 10:40:00 GMT</pubDate>
<guid>999999</guid>
</item>
</channel>
</rss>

Now when I run my script it errors with:

explode() expects parameter 2 to be string, array given in D:\wamp\www\xpathtest.php on line 9

Now, I assume that the error means that my $result is an array and needs to be a string?
 
No, its just a single figure that is mixed within text of the <description></description> that is within one of the <br></br> tags

The proper rss is something like this:

<description>
<br>15:00 pm GMT</br>
<br>25,000</br>
<br>Kick-off</br>
</description>
 
Last edited:
Back
Top Bottom