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?
 
This is my code:

PHP:
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?

Yes. Your $result is this:

Code:
array(2) {
  [0]=>
  object(SimpleXMLElement)#2 (1) {
    [0]=>
    string(18) "
football results
"
  }
  [1]=>
  object(SimpleXMLElement)#3 (1) {
    ["br"]=>
    array(2) {
      [0]=>
      string(18) "SCRAPE THIS NUMBER"
      [1]=>
      string(15) "IN HERE 5000000"
    }
  }
}

To further clarify, the <br> tags are being parsed as XML, and broke up into an array. I presume you expected to be returned a string? Not sure what you want to do, but I presume your code is trying to do this:

Code:
<?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");

$number ='';
foreach($result[1] as $br)
{
        $number .= $br;
}
//removes every character & symbol except numbers
$number = preg_replace("/\D/", "", $number);

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

Code:
php test.php 
5000000
 
Last edited:
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:
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>

I'm probably being stupid, but whats wrong with this?

Code:
<?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");

$number ='';
foreach($result[1] as $br)
{
        $number .= $br;
}

echo $number;

Or rather, if the layout is consistent and you want a specific entry:

Code:
<?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");

echo $result[1]->br[1];
 
Last edited:
Back
Top Bottom