且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-08-25 13:27:46

试试:
innertext()用于读取或写入元素的内部HTML文本的innertext。

  foreach($ html-> find('。name a')as $ element)
{
echo< br>标签文本值=。 $元素 - &GT;的innerText;
}

API Ref


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!

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