2010-02-22 116 views
0

正如我在前面的问题中所建议的,我使用PHP Simple HTML DOM Parser 作为搜索HTML文档并从特定元素(在本例中为textarea)抓取内容的一种方法。我想知道是否有人曾经使用过,并能够给我任何建议(或推荐其他解决方案)。该代码我用这样的容貌:PHP DOMParser帮助

include "../simple_html_dom.php"; 
$html  = file_get_html('http://example.com'); 
$textarea = $html->find('textarea[id=body]'); 
$contents = $textarea->outertext; 

echo $contents; 

我的问题是,脚本运行的代码,并从元素检索任何数据(这看起来是这样的:

<textarea id="body" name="body" rows="12" cols="75" tabindex="3"> 
At 2/21/10 10:15 PM, You wrote 
: Hello world, 
: how are you today? 

</textarea> 

对不起如果其他人有任何建议,将不胜感激。非常感谢你提前

回答

0

正如你可以在documentation看到find返回一个数组,所以对于初学者你不应该它就好像它是一个节点。此外,你想要的是innertext而不是outertext

这应该工作:

include "../simple_html_dom.php"; 
$html  = file_get_html('http://example.com'); 
$textarea = $html->find('textarea[id=body]'); 
$contents = $textarea[0]->innertext; 

echo $contents; 
0

您链接到的网页显示如下:

// Find all <div> which attribute id=foo 
$ret = $html->find('div[id=foo]'); 

这表明,我认为$ret将是一个集合。在句法上,您使用的是与CSS attribute selectors类似的内容。由于此语法旨在推广到适用于任何属性,事实上,id应该是唯一的不考虑和集合返回...我怀疑,如果您尝试这样的事情...

$ret = $html->find('div#foo'); 

......它可能工作。