正如我在上一个问题中所建议的,我使用PHP Simple HTML DOM Parser作为搜索HTML文档并从特定元素(在我的示例中是文本区域)获取内容的一种方法。我想知道以前是否有人使用过这个工具,并能为我的问题提供任何建议(或推荐其他解决方案)。我使用的代码如下所示:
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>我很抱歉用这种方法寻求建议,但如果其他人有任何建议,我将不胜感激。提前谢谢你
发布于 2010-02-22 13:41:21
正如您在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;发布于 2010-02-22 13:44:02
您链接到的页面显示以下内容:
// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]'); 对我来说,这暗示着$ret将是一个集合。从语法上讲,您使用的是类似于CSS attribute selectors的东西。由于此语法旨在推广应用于任何属性,因此不考虑id应该是唯一的这一事实,并返回一个集合...我怀疑如果你尝试过这样的事情...
$ret = $html->find('div#foo'); ..。也许能行得通。
https://stackoverflow.com/questions/2309003
复制相似问题