我使用file_get_html()
来获取一些外部HTML,但我遇到了问题。我似乎无法在div中定位文本,同时避免获取剩余的内容。
让我们说布局是这样的:
<div class="post">
<h1>Andromeda v1.4 – WordPress – The Beauty of Simplicity</h1>
<div class="infos b20">
<img class="post_img" src="/imagini/512b93babf84b.jpg" alt="Andromeda v1.4 – WordPress – The Beauty of Simplicity">
<div style="width:610px; margin:10px 0; overflow:hidden; display:block;">
enter code here
Andromeda is a clean theme with functional CMS and unique features. A massive pack of backend CMS options was created for this product to give you full control while creating and editing the site and its features. The main idea behind this theme was to create a something clean and simple, useful, nice looking and easy to modify.
<p></p>
<h6>Demo</h6>
<code>http://themeforest.net/item/andromeda-wordpress-the-beauty-of-simplicity/107876</code>
<h6>Download:</h6>
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<p></p>
<ul id="social_post" class="clearfix sharingbtns">
<div class="comments">
</div>
如果我做了
$text = $dom->find('div[class=post]');
$text = $text[0]->plaintext;
我得到了所有内容,我只想要文本,在主要div中使用类帖子,而不是所有其他内容。
实现这个目标的最佳途径是什么?
其他div的文本和数量是可变的,但div类的帖子和文本总是在那里,处于相同的位置。
编辑:详细说明一下,我只想要帖子内的文字,并且没有标签
答案 0 :(得分:3)
只是快速回答你而不检查它是否有效:
http://simplehtmldom.sourceforge.net/manual_api.htm
试试这个:
$text = $dom->find('div[class=post]');
$text = $text[0]->innertext;
或:
$text = $dom->find('div[class=post]');
$text = $text[0]->outertext;
顺便说一下:
<div style="width:610px; margin:10px 0; overflow:hidden; display:block;">
没有结束标记,所以DIV中没有任何文字你正在谈论。请澄清。
答案 1 :(得分:0)
$res = $html->find('div[class=post]',0)->plaintext;