2013-03-18 84 views
0

我使用file_get_html()来获取一些外部HTML,但我有一个问题。我似乎无法在div中定位文本,同时避免获取其余内容。file_get_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> 

如果我做了

我得到的所有的内容,我只想文本,与类岗位主要DIV中,而不是所有其他内容。

达到此目的的最佳方法是什么?

其他div的文本和数量是可变的,但div类的帖子和文本将始终在相同的位置。

编辑:为了阐述,我只希望内部发布文字多数民众赞成,且没有标签

回答

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你里面;再谈论。请澄清。

0
$res = $html->find('div[class=post]',0)->plaintext; 
相关问题