Get text from next tag

╄→尐↘猪︶ㄣ 提交于 2020-01-21 11:43:08

问题


I have a html snippet that looks like this (of course surrounded by other html):

<p class="finfot3"><b>Header:</b></p>
<p>Text</p>

How can I get Text from this? I'm using simple_html_dom, but I can use something else if simple_html_dom can't do this.


回答1:


This is untested, but you might be looking for simple_html_doms next_sibling() method.

$html->find('p[class=finfot3]')->next_sibling()->innertext() should return the contents of the second <p> element.




回答2:


Find the p element with the class. Then use

  • $e->next_sibling() - Returns the next sibling of element, or null if not found.

where $e is the element with the class.

For better alternatives to SimpleHtmlDom, see Best Methods to parse HTML




回答3:


preg_match('~<p>(.*)</p>~', $html, $matches);
var_dump($matches);


来源:https://stackoverflow.com/questions/4920695/get-text-from-next-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!