derekshull
derekshull

Reputation: 305

Code not fully parsing data

I have the following code that looks at the wolfram alpha xml data and pull out the answer surrounded in the <plaintext> tags. It works for the first tag, but not for after the answer.

Code:

<?php
$url = "http://api.wolframalpha.com/v2/query?input=what+day+is+today&appid=9QA6R9-VGL4AAURHU&format=plaintext&podtitle=Result";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
ob_start();
curl_exec($ch);
curl_close($ch);
$contents = ob_get_contents();
ob_end_clean();
echo $contents;

Here is the answer I get:

Monday, May 3, 2011</plaintext>
  </subpod>
 </pod>
 <sources count='2'>
  <source url='http://www.wolframalpha.com/sources/AstronomicalDataSourceInformationNotes.html'
      text='Astronomical data' />
  <source url='http://www.wolframalpha.com/sources/PeopleDataSourceInformationNotes.html'
      text='People data' />
 </sources>
</queryresult>

Upvotes: 0

Views: 111

Answers (1)

Dave Chen
Dave Chen

Reputation: 10975

Try this:

<?php
$url = "http://api.wolframalpha.com/v2/query?input=what+day+is+today&appid=9QA6R9-VGL4AAURHU&format=plaintext&podtitle=Result";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
ob_start();
curl_exec($ch);
curl_close($ch);
$contents = ob_get_contents();
ob_end_clean();

function get($a,$b,$c){
     $y = explode($b,$a);
     $x = explode($c,$y[1]);
    return $x[0];
}

echo get($contents,'<plaintext>','</plaintext>');
?>

Upvotes: 2

Related Questions