开发者

PHP DOMParser Help

As recommended in a previous question of mine, I am using the PHP Simple HTML DOM Parser as a way to search an HTML document and grab contents fr开发者_StackOverflowom a specific element (in my case, a textarea). I was wondering if anyone had used this before and was able to give me any advice for my problem (or recommend another solution). The code I'm using looks like this:

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

echo $contents;

My problem is that the script runs the code and retrieves no data from the element (which looks like this:

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

I'm sorry to ask for advice with this certain method, but if anyone else has any advice it would be greatly appreciated. Thank you so much in advance


As you can see in the documentation find returns an array, so for starters you shouldnt treat it as if it were a node. Also, what you want is innertext not outertext.

This should work:

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

echo $contents;


The page you linked to says the following:

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

This suggests to me that $ret would be a collection. Syntactically, you're using something similar to CSS attribute selectors. Since this syntax is meant to be generalized to apply to any attribute, the fact that id should be unique isn't considered and a collection is returned... I suspect that if you tried something like this...

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

... it might work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜