开发者

Get element content from a variable containing html

How do I use the DOM parser to extract the content of a html element in a variable.

More exactly: I have a form where user inputs html in a te开发者_运维百科xt area. I want to extract the content of the first paragraph.

I know there are many tutorials on this, but could not find any on extracting from variable and not a file(page)

Thanks


If you're taking HTML as user input, I recommend using simplehtmldom. It has a loose parser with tolerance for buggy html and lets you use CSS selectors to pull element and their content out of the DOM.

I didn't test this, but it should work:

$html = str_get_html($_POST['input']);
print $html->find('p:first')->plaintext; // first paragraph
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜