2

我是 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 对象 ( [nodetype] => 1 [tag] => img [attr] => 数组 ( [onerror] => img_onerror(this); [data-error-url] => http: //img1a.flixcart.com/img/book.jpg [高度] => 275 [宽度] => 275 [数据-src] => http://img7a.flixcart.com/img/622/9781846467622.jpg [src] => data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP////Ly8v///yH5BAEAAAMALAAAAAAAAABAAEAAAICVAEAOw== [onload] => lzld(this) [alt] => Buy Numbers [title] => Numbers ) [children ] => 数组 ( ) [节点] => 数组 ( )

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

萤火虫看到的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>
4

1 回答 1

2

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

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

现场演示

于 2013-10-23T21:04:28.050 回答