Milos Cuculovic
Milos Cuculovic

Reputation: 20223

How to keep child nodes tags in Symfony2 DomCrawler

I am using Symfony2 DomCrawler to serch for specific nodes.

I have a DOMDocument with some html inside. What I am doing basicaly is that I am searching for a <p> tag with the specific class name.

Let's say I have this html in the $dom object:

<p class="one">class one</p>
<p class="two">class two is the <b>good</b> class</p>
<p class="tree">class tree</p>
<p class="four">class four</p>

I am then using

$crawler    = new Crawler($dom);
$class      = 'two';
$paragraphs = $crawler->filterXPath('//p');

foreach( $paragraphs as $paragraph ) {
        if ( $paragraph->hasAttribute('class') === false ) {
            continue;
        }

        $class = $paragraph->getAttribute('class');

        if($class == $class_name){
            $node_value = $paragraph->nodeValue;
        }

The problem is that here, I am getting

class two is the good class

And I would like to get

class two is the <b>good</b> class

How to keep those <b></b> tags in the result?

Upvotes: 2

Views: 1529

Answers (1)

Touki
Touki

Reputation: 7525

This is because <b></b> is a subnode and ->nodeValue only takes its content.
You need to fetch the content of the child nodes as mentioned in another question

This sample works in your case

$dom = <<<'STR'
<p class="one">class one</p>
<p class="two">class two is the <b>good</b> class</p>
<p class="tree">class tree</p>
<p class="four">class four</p>
STR;

$crawler    = new Crawler($dom);
$class_name = 'two';
$paragraphs = $crawler->filterXPath('//p');

foreach ($paragraphs as $paragraph) {
    if (false === $paragraph->hasAttribute('class')) {
        continue;
    }

    $class = $paragraph->getAttribute('class');

    if ($class == $class_name) {
        $value = '';

        foreach ($paragraph->childNodes as $child) {
            $value .= $paragraph->ownerDocument->saveHTML($child);
        }
    }
}

echo $value; // class two is the <b>good</b> class

Upvotes: 2

Related Questions