1

I want to get the IMG SRC tag from the following code:

<pod title="Scientific name" scanner="Data" id="ScientificName:SpeciesData" position="200" error="false" numsubpods="1">
<subpod title="">
<plaintext>Canis lupus familiaris</plaintext>
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP17941cd1c5fi21h72ac2000057i1ae7gc4986gdf?MSPStoreType=image/gif&s=44" alt="Canis lupus familiaris" title="Canis lupus familiaris" width="139" height="18"/>
</subpod>
</pod>

I know how to get the plaintext information but how do I get the img src information? Here's what I have to get the plaintext information:

<?php
if(isset($_POST['q'])){
include 'WolframAlphaEngine.php';
$engine = new WolframAlphaEngine( 'APP-ID' );

$resp = $engine->getResults("$q");

$pod = $resp->getPods();

$pod1 = $pod[1];


foreach($pod1->getSubpods() as $subpod){
  if($subpod->plaintext){
    $plaintext = $subpod->plaintext;
    break;
  }
}

$result = substr($plaintext, 0,strlen($plaintext)-3);

echo "$plaintext";

}
?>

It's not a duplicate of Grabbing the href attribute of an A element because I can't use DOM on my Godaddy hosting. I've tried it before.

Community
  • 1
  • 1
user2362601
  • 341
  • 2
  • 5
  • 13

1 Answers1

0

I've never worked with that WolframAlphaEngine before. I assume it's this one:

If so, have a look at https://github.com/brkeerthi/chethana/blob/master/include/WASubpod.php

class WASubpod {
  // define the sections of a response
  public $attributes = array();
  public $plaintext = '';
  public $image = '';
  public $minput = '';
  public $moutput = '';
  public $mathml = '';

As you can see, it has properties not only for plaintext, but also for attributes and other stuff. It might be as easy as

foreach ($pod1->getSubpods() as $subpod) {
    $attributes = $subpod->attributes;
    if (isset($attributes['src']) {
        echo $attributes['src'];
    }
}

But since the documentation of that library is non-existent, I cannot tell. So if this doesn't work, just var_dump the $subpod to see what it contains.

Likewise, have a look at the other classes to find out what they do:

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Very close! I did your code and it came up with just an array [title] thing...so I did `foreach($pod1->getSubpods() as $subpod){ print_r($subpod->image); }` instead of atrributes and it gives me ` WAImage Object ( [attributes] => Array ( [src] => http://www4b.wolframalpha.com/Calculate/MSP/MSP7141hbbg7330dcd6h4f00002cb39d647363ei1g?MSPStoreType=image/gif&s=55 [alt] => Canis lupus familiaris [title] => Canis lupus familiaris [width] => 139 [height] => 18 ) )` now how do i get the src from that? – user2362601 May 08 '13 at 14:24
  • @user2362601 according to the dump: `$subpod->image->attributes['src']` should work – Gordon May 08 '13 at 14:33