【问题标题】:Get specific tag from api code从 api 代码中获取特定标签
【发布时间】:2013-05-08 13:44:36
【问题描述】:

我想从以下代码中获取IMG SRC标签:

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

我知道如何获取明文信息,但如何获取 img src 信息?这是我必须获取明文信息的内容:

<?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";

}
?>

这不是 Grabbing the href attribute of an A element 的副本,因为我无法在 Godaddy 主机上使用 DOM。我以前试过。

【问题讨论】:

    标签: php html image tags src


    【解决方案1】:

    我以前从未使用过那个 WolframAlphaEngine。我猜是这个:

    如果是这样,请查看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 = '';
    

    如您所见,它不仅具有纯文本属性,还具有属性和其他内容。这可能就像

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

    但由于该库的文档不存在,我无法确定。因此,如果这不起作用,只需 var_dump $subpod 即可查看其中包含的内容。

    同样,查看其他类以了解它们的作用:

    【讨论】:

    • 非常接近!我做了你的代码,它只提出了一个数组 [title] 的东西......所以我做了 foreach($pod1-&gt;getSubpods() as $subpod){ print_r($subpod-&gt;image); } 而不是 atrributes 它给了我` WAImage Object ( [attributes] => Array ( [src] => @987654324 @ [alt] => Canis lupus familiaris [title] => Canis lupus familiaris [width] => 139 [height] => 18 ) )` 现在我如何从中获取 src?
    • @user2362601 根据转储:$subpod-&gt;image-&gt;attributes['src'] 应该可以工作
    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 2021-12-14
    • 2022-12-17
    • 2012-11-15
    • 2021-07-08
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多