【问题标题】:Echo an item from an array in php从 php 的数组中回显一个项目
【发布时间】:2013-11-02 07:12:29
【问题描述】:

我是 php 新手,在从以下 php 代码生成的数组中提取一些字段时遇到了一个典型问题:

$html=file_get_html("http://www.flipkart.com/books/9781846467622");

$e = $html->getElementById("mprodimg-id");
echo $e;
$f = $e->find('img');
echo $f['data-src'];

在代码中应用var_dump($f)后看到的输出数组f如下:-

数组 ( [0] => simple_html_dom_node 对象 ( [节点类型] => 1 [标签] => img [attr] => 数组 ( [onerror] => img_onerror(this); [数据错误网址] => http://img1a.flixcart.com/img/book.jpg [身高] => 275 [宽度] => 275 [数据源] => http://img7a.flixcart.com/img/622/9781846467622.jpg [src] => 数据:图像/gif;base64,R0lGODlhAQABAJEAAAAAAP////Ly8v///yH5BAEAAAMALAAAAAAAABAAEAAAICVAEAOw== [onload] => lzld(这个) [alt] => 购买号码 [标题] => 数字 ) [孩子] => 数组 ( ) [节点] => 数组 ( )

我需要回显字段“data-src”的值。有人可以帮忙吗?

firebug 看到的 html 如下,我需要从这里抓取它。

<div id="mprodimg-id" class="mprodimg">             
<img width="275" height="275" title="Numbers" alt="Buy Numbers" onload="lzld(this)" src="data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP////Ly8v///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==" data-src="http://img7a.flixcart.com/img/622/9781846467622.jpg" data-error-url="http://img1a.flixcart.com/img/book.jpg" onerror="img_onerror(this);">
</div>

【问题讨论】:

  • file_get_html 函数是做什么的?
  • 它将url作为html存储在$html变量中。

标签: php html arrays simple-html-dom


【解决方案1】:

这是一个干净的工作示例:

// includes Simple HTML DOM Parser
include "simple_html_dom.php";

$url = "http://www.flipkart.com/numbers/p/9781846467622?pid=9781846467622";

//Create a DOM object
$dom = new simple_html_dom();
// Load HTML from url
$dom->load_file($url);


// Find the wanted image using the appropriate selectors
$img = $dom->find('#mprodimg-id img', 0);


// Find succeeded
if ($img){
    echo $img->{'data-src'};
}
else
    echo "Find function failed !";


// Clear DOM object (needed essentially when using many)
$dom->clear(); 
unset($dom);

OUTPUT
======
http://img7a.flixcart.com/img/622/9781846467622.jpg

Live DEMO

【讨论】:

猜你喜欢
  • 2013-10-23
  • 1970-01-01
  • 2019-05-17
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
  • 2014-11-10
相关资源
最近更新 更多