dqrzot2791 2016-04-02 09:04
浏览 44
已采纳

PHP简单HTML DOM - 如何获取标记内的文本

I use PHP Simple HTML Dom to get some HTML, now I have a HTML dom like follow code, I need fetch the plain text inner a tag, but I am getting the text link(Kiwi Fruit Basket).

HTML Code

<div class="name" style="height: 34px;">
    <a href="http://floristchennai.com/kiwi-basket">Kiwi Fruit Basket</a>
</div>

Php Code

 // Create DOM from URL or file
 $html = file_get_html('http://floristchennai.com/');

 // Find all links text
 foreach($html->find('.name a') as $element) 
 {
    echo "<br>a tag text value=" . $element;
 }

Doing it this way I don't get the text I want to get.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dsbtwy1329 2016-04-02 09:09
    关注

    try: innertext() innertext used for Read or write the inner HTML text of element.

        foreach($html->find('.name a') as $element) 
        {
            echo "<br>a tag text value=" . $element->innertext;
        }
    

    API Ref

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?