DOMDocument : how to get inner HTML as Strings separated by line-breaks?

巧了我就是萌 提交于 2020-01-05 03:37:30

问题


<blockquote>
 <p>
   2 1/2 cups sweet cherries, pitted<br>
   1 tablespoon cornstarch <br>
   1/4 cup fine-grain natural cane sugar
 </p>
</blockquote>

hi , i want to get the text inside 'p' tag . you see there are three different line and i want to print them separately after adding some extra text with each line . here is my code block

    $tags = $dom->getElementsByTagName('blockquote');
    foreach($tags as $tag)
    {
        $datas = $tag->getElementsByTagName('p');
        foreach($datas as $data)
        {
            $line = $data->nodeValue;
            echo $line;
        }
    } 

main problem is $line contains the full text inside 'p' tag including 'br' tag . how can i separate the three lines to treat them respectively ??

thanks in advance.


回答1:


You can do that with XPath. All you have to do is query the text nodes. No need to explode or something like that:

$dom = new DOMDocument;
$dom->loadHtml($html);
$xp = new DOMXPath($dom);
foreach ($xp->query('/html/body/blockquote/p/text()') as $textNode) {
    echo "\n<li>", trim($textNode->textContent);
}

The non-XPath alternative would be to iterate the children of the P tag and only output them when they are DOMText nodes:

$dom = new DOMDocument;
$dom->loadHtml($html);
foreach ($dom->getElementsByTagName('p')->item(0)->childNodes as $pChild) {
    if ($pChild->nodeType === XML_TEXT_NODE) {
        echo "\n<li>", trim($pChild->textContent);
    }
}

Both will output (demo)

<li>2 1/2 cups sweet cherries, pitted
<li>1 tablespoon cornstarch
<li>1/4 cup fine-grain natural cane sugar

Also see DOMDocument in php for an explanation of the node concept. It's crucial to understand when working with DOM.




回答2:


You can use

$lines = explode('<br>', $data->nodeValue);



回答3:


here is a solution in javascript syntax

 var tempArray = $line.split("<br>");  

echo $line[0]
echo $line[1]
echo $line[2]



回答4:


You can use the php explode function like this. (assuming each line in your <p> tag ends with <br>)

$tags = $dom->getElementsByTagName('blockquote');
foreach($tags as $tag)
{
    $datas = $tag->getElementsByTagName('p');
    foreach($datas as $data)
    {
        $contents = $data->nodeValue;
        $lines = explode('<br>',$contents);
        foreach($lines as $line) {
            echo $line;
        }
    }
} 


来源:https://stackoverflow.com/questions/15636739/how-to-get-innercontent-with-domdocument

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