2013-05-08 133 views
0

我有以下的代码,获取来自WolframAlpha的API的一些信息:获取IMG SRC信息显示图像

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

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

$pod = $resp->getPods(); 

$pod1 = $pod[1]; 


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

$pod2 = $pod[2]; 

foreach($pod2->getSubpods() as $subpod2){ 
    if($subpod2->plaintext){ 
    $plaintext2 = $subpod2->plaintext; 
    break; 
    } 
} 

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

preg_match('|img src="([^"]+)"|', $plaintext, $matches); 
$img_src = $matches[1]; 

echo "$img_src<br><img src='$img_src'>"; 
echo "<br><br>"; 
echo $plaintext2; 

} 
?> 

有每个subpod内的img标签。我如何获得img src并显示图像?

我知道这是内这里:

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



这里是上下文:

<queryresult success="true" error="false" numpods="10" datatypes="Species" timedout="" timedoutpods="" timing="2.944" parsetiming="0.076" parsetimedout="false" recalculate="" id="MSPa17911cd1c5fi21h72ac20000332e54b60gcg1bff" host="http://www4a.wolframalpha.com" server="44" related="http://www4a.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa17921cd1c5fi21h72ac20000535d27652696856c&s=44" version="2.6"> 
<pod title="Input interpretation" scanner="Identity" id="Input" position="100" error="false" numsubpods="1"> 
<subpod title=""> 
<plaintext>dog (animal)</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP17931cd1c5fi21h72ac2000021h88ec64ehf1h10?MSPStoreType=image/gif&s=44" alt="dog (animal)" title="dog (animal)" width="89" height="18"/> 
</subpod> 
</pod> 
<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> 
<pod title="Alternate scientific names" scanner="Data" id="AlternateScientificNames:SpeciesData" position="300" error="false" numsubpods="1"> 
<subpod title=""> 
<plaintext>Canis familiaris | Canis familiaris domesticus</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP17951cd1c5fi21h72ac200004345f656d2010iea?MSPStoreType=image/gif&s=44" alt="Canis familiaris | Canis familiaris domesticus" title="Canis familiaris | Canis familiaris domesticus" width="301" height="18"/> 
</subpod> 
</pod> 
<pod title="Taxonomy" scanner="Data" id="Taxonomy:SpeciesData" position="400" error="false" numsubpods="1"> 
<subpod title=""> 
<plaintext> 
kingdom | Animalia (animals) phylum | Chordata (chordates) class | Mammalia (mammals) order | Carnivora (carnivores) family | Canidae (coyotes, dogs, foxes, jackals, wolves...) genus | Canis species | Canis lupus (gray wolf) 
</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP17961cd1c5fi21h72ac200003ec116f8g9gh4274?MSPStoreType=image/gif&s=44" alt="kingdom | Animalia (animals) phylum | Chordata (chordates) class | Mammalia (mammals) order | Carnivora (carnivores) family | Canidae (coyotes, dogs, foxes, jackals, wolves...) genus | Canis species | Canis lupus (gray wolf)" title="kingdom | Animalia (animals) phylum | Chordata (chordates) class | Mammalia (mammals) order | Carnivora (carnivores) family | Canidae (coyotes, dogs, foxes, jackals, wolves...) genus | Canis species | Canis lupus (gray wolf)" width="424" height="228"/> 
</subpod> 
<states count="1"> 
<state name="More" input="Taxonomy:SpeciesData__More"/> 
</states> 
</pod> 
<pod title="Biological properties" scanner="Species" id="SpeciesDataPhysicalProperties" position="500" error="false" numsubpods="4"> 
<subpod title="Basic properties"> 
<plaintext>lifespan | (15 to 20) years</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP17971cd1c5fi21h72ac2000067054fficad60e67?MSPStoreType=image/gif&s=44" alt="lifespan | (15 to 20) years" title="lifespan | (15 to 20) years" width="205" height="36"/> 
</subpod> 
<subpod title="Sensory organs"> 
<plaintext> 
eyeball diameter | 0.83 inches (human: 0.94 in) eyeball volume | 0.31 in^3 (cubic inches) (human: 0.4 in^3) visual field | 250° (degrees) eardrum surface area | 46 mm^2 (square millimeters) (human: 55 mm^2) angle of auditory acuity | 2.5° (degrees) (human: 8.4°) upper limit of hearing ability | 140 kHz (kilohertz) (human: 21 kHz) olfactory epithelium receptor count | 2.3×10^8 (human: 3×10^7) olfactory epithelium surface area | 13 in^2 (square inches) (human: 0.39 to 0.78 in^2) 
</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP17981cd1c5fi21h72ac2000053f883b59b99g4c4?MSPStoreType=image/gif&s=44" alt="eyeball diameter | 0.83 inches (human: 0.94 in) eyeball volume | 0.31 in^3 (cubic inches) (human: 0.4 in^3) visual field | 250° (degrees) eardrum surface area | 46 mm^2 (square millimeters) (human: 55 mm^2) angle of auditory acuity | 2.5° (degrees) (human: 8.4°) upper limit of hearing ability | 140 kHz (kilohertz) (human: 21 kHz) olfactory epithelium receptor count | 2.3×10^8 (human: 3×10^7) olfactory epithelium surface area | 13 in^2 (square inches) (human: 0.39 to 0.78 in^2)" title="eyeball diameter | 0.83 inches (human: 0.94 in) eyeball volume | 0.31 in^3 (cubic inches) (human: 0.4 in^3) visual field | 250° (degrees) eardrum surface area | 46 mm^2 (square millimeters) (human: 55 mm^2) angle of auditory acuity | 2.5° (degrees) (human: 8.4°) upper limit of hearing ability | 140 kHz (kilohertz) (human: 21 kHz) olfactory epithelium receptor count | 2.3×10^8 (human: 3×10^7) olfactory epithelium surface area | 13 in^2 (square inches) (human: 0.39 to 0.78 in^2)" width="496" height="328"/> 
</subpod> 
<subpod title="Internal organs"> 
<plaintext> 
brain weight | 4.8 oz (ounces) (human: 2.6 to 3.3 lb) heart weight | 4.8 oz (ounces) (human: 11 oz) cardiac output | (120 to 180) in^3/min (inches cubed per minute) (human: 340 to 370 in^3/min) interior lung surface area | 970 ft^2 (square feet) (human: 970 ft^2) stomach capacity | 1.1 gallons digestive tract capacity | 1.8 gallons length of whole intestine | 16 feet (human: 20 to 26 ft) small intestine capacity | 55 fl oz (fluid ounces) large intestine and rectum capacity | 31 fl oz (fluid ounces) appendix capacity | 3 fl oz (fluid ounces) 
</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP17991cd1c5fi21h72ac200005ec0d00i290h0gf3?MSPStoreType=image/gif&s=44" alt="brain weight | 4.8 oz (ounces) (human: 2.6 to 3.3 lb) heart weight | 4.8 oz (ounces) (human: 11 oz) cardiac output | (120 to 180) in^3/min (inches cubed per minute) (human: 340 to 370 in^3/min) interior lung surface area | 970 ft^2 (square feet) (human: 970 ft^2) stomach capacity | 1.1 gallons digestive tract capacity | 1.8 gallons length of whole intestine | 16 feet (human: 20 to 26 ft) small intestine capacity | 55 fl oz (fluid ounces) large intestine and rectum capacity | 31 fl oz (fluid ounces) appendix capacity | 3 fl oz (fluid ounces)" title="brain weight | 4.8 oz (ounces) (human: 2.6 to 3.3 lb) heart weight | 4.8 oz (ounces) (human: 11 oz) cardiac output | (120 to 180) in^3/min (inches cubed per minute) (human: 340 to 370 in^3/min) interior lung surface area | 970 ft^2 (square feet) (human: 970 ft^2) stomach capacity | 1.1 gallons digestive tract capacity | 1.8 gallons length of whole intestine | 16 feet (human: 20 to 26 ft) small intestine capacity | 55 fl oz (fluid ounces) large intestine and rectum capacity | 31 fl oz (fluid ounces) appendix capacity | 3 fl oz (fluid ounces)" width="496" height="409"/> 
</subpod> 
<subpod title=""> 
<plaintext>(typical values, except where otherwise noted)</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP18001cd1c5fi21h72ac200005210724bc1978f7h?MSPStoreType=image/gif&s=44" alt="(typical values, except where otherwise noted)" title="(typical values, except where otherwise noted)" width="249" height="20"/> 
</subpod> 
<states count="2"> 
<state name="Show metric" input="SpeciesDataPhysicalProperties__Show metric"/> 
<state name="More" input="SpeciesDataPhysicalProperties__More"/> 
</states> 
</pod> 
<pod title="Genome information" scanner="Data" id="GenomeSummary:SpeciesData" position="600" error="false" numsubpods="1"> 
<subpod title=""> 
<plaintext> 
total base pairs | 2445126910 total chromosomes | 39 known genes | 19528 known RNAs | 16953 known proteins | 33215 
</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP18011cd1c5fi21h72ac200006823c4b9b14245f8?MSPStoreType=image/gif&s=44" alt="total base pairs | 2445126910 total chromosomes | 39 known genes | 19528 known RNAs | 16953 known proteins | 33215" title="total base pairs | 2445126910 total chromosomes | 39 known genes | 19528 known RNAs | 16953 known proteins | 33215" width="260" height="164"/> 
</subpod> 
<states count="1"> 
<state name="Show details" input="GenomeSummary:SpeciesData__Show details"/> 
</states> 
</pod> 
<pod title="Image" scanner="Data" id="Image:SpeciesData" position="700" error="false" numsubpods="1"> 
<subpod title=""> 
<plaintext/> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP18021cd1c5fi21h72ac200004030c96gcgd02f40?MSPStoreType=image/gif&s=44" alt="" title="" width="150" height="120"/> 
</subpod> 
</pod> 
<pod title="Species authority" scanner="Data" id="SpeciesAuthorithy:SpeciesData" position="800" error="false" numsubpods="1"> 
<subpod title=""> 
<plaintext>Linnaeus</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP18031cd1c5fi21h72ac200002c691ead7fc4927e?MSPStoreType=image/gif&s=44" alt="Linnaeus" title="Linnaeus" width="60" height="18"/> 
</subpod> 
</pod> 
<pod title="Other members of species Canis lupus" scanner="Species" id="SpeciesScannerOtherMembersOfSpecies" position="900" error="false" numsubpods="1"> 
<subpod title=""> 
<plaintext> 
Canis lupus subsp. lupus | Canis lupus subsp. lycaon 
</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP18041cd1c5fi21h72ac200000e76cgidc1b4a4fi?MSPStoreType=image/gif&s=44" alt="Canis lupus subsp. lupus | Canis lupus subsp. lycaon" title="Canis lupus subsp. lupus | Canis lupus subsp. lycaon" width="345" height="18"/> 
</subpod> 
</pod> 
<pod title="Taxonomic network" scanner="Data" id="TaxonomyGraph:SpeciesData" position="1000" error="false" numsubpods="1"> 
<subpod title=""> 
<plaintext/> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP18051cd1c5fi21h72ac200002ff8ihic491e8e59?MSPStoreType=image/gif&s=44" alt="" title="" width="496" height="280"/> 
</subpod> 
<states count="1"> 
<state name="Show as network" input="TaxonomyGraph:SpeciesData__Show as network"/> 
</states> 
</pod> 
<assumptions count="1"> 
<assumption type="Clash" word="dog" template="Assuming "${word}" is ${desc1}. Use as ${desc2} instead" count="3"> 
<value name="Species" desc="a species specification" input="*C.dog-_*Species-"/> 
<value name="DogBreedClass" desc="a class of dog breeds" input="*C.dog-_*DogBreedClass-"/> 
<value name="ExamplePage" desc="a general topic" input="*C.dog-_*ExamplePage-"/> 
</assumption> 
</assumptions> 
<sources count="2"> 
<source url="http://www.wolframalpha.com/sources/GenomeSequenceDataSourceInformationNotes.html" text="Genome sequence data"/> 
<source url="http://www.wolframalpha.com/sources/SpeciesDataSourceInformationNotes.html" text="Species data"/> 
</sources> 
</queryresult> 

我想要得到的IMG SRC和显示图像。

+0

你能提供$纯文本的示例内容? – Johan 2013-05-08 05:25:12

+0

我更新了我的帖子以获取代码。这有帮助吗? – derekshull 2013-05-08 13:27:51

回答

1

你可以使用DomDocument类这样的,其中$content是包含要工作,对<img>标签的字符串。

$dom = new DomDocument(); 
@$dom->loadHTML($content); 
$images = $dom->getElementsByTagName('img'); 

foreach ($images as $img) { 
    echo $img->getAttribute('src'); 
} 

编辑: $content应该是:

<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> 
+0

这没有奏效。它没有给我从豆荚的img,而是另一张图片。 – derekshull 2013-05-08 14:04:40

+0

是否需要将$ content作为我所从地点的url? – derekshull 2013-05-08 14:05:24

+0

@derekshull请参阅编辑。代码片段的功能是**在'$ content'内搜索'img'标签,并提取它的'src'属性**。 – 2013-05-08 14:08:45