First getElementsByTagName () returns all elements in HTML (strange behavior)

I am using PHP to parse HTML provided to me by Wordpress.

This PHP post was returned by my Wordpress:

<p>Test</p> 
<p>
    <img class="alignnone size-thumbnail wp-image-39" src="img.png"/>
</p> 
<p>Ok.</p>

      

This is my parsing function (debugged on the left):

function get_parsed_blog_post()
{
    $html = ob_wp_content(false);

    print_r(htmlspecialchars($html));
    echo '<hr/><hr/><hr/>';

    $parse = new DOMDocument();
    $parse->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

    $xpath = new DOMXpath($parse);
    $ps = $xpath->query('//p');

    foreach ($ps as $p) 
    {
        $imgs = $p->getElementsByTagName('img');

        print($imgs->length);
        echo '<br/>';

        if ($imgs->length > 0)
        {
            $p->setAttribute('class', 'image-content');

            foreach ($imgs as $img)
            {
                $img->removeAttribute('class');
            }
        }        
    }

    $htmlFinal = $parse->saveHTML();

    print_r(htmlspecialchars($htmlFinal));
    echo '<hr/><hr/><hr/>';

    return $htmlFinal;
}

      

The purpose of this code is to remove the classes that Wordpress adds to <img>

s and set any <p>

that contains an image as a class image-content

.

And this returns:

1
1
0
<p class="image-content">Test
<p class="image-content">
    <img src="img.png">
</p>
<p>Ok.</p></p>

      

Anyway, it ended up first appearing <p>

around all of my parsed mail, causing the first to <p>

have the class image-content

misapplied. Why is this happening? How do I stop him?

+3


source to share


1 answer


METHOD 1

As far as using your code specifically, I made some changes to make it work.

If you print each one $p

, you should be able to see that the first element will contain all of your HTML. The simplest solution is to add an empty one <p>

before your HTML and skip it when foreach

.

function get_parsed_blog_post()
{
    $page_content_html = ob_wp_content(false);
    $html = "<p></p>".$page_content_html;
    print_r(htmlspecialchars($html));
    echo '<hr/><hr/><hr/>';

    $parse = new DOMDocument();
    $parse->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

    $xpath = new DOMXpath($parse);
    $ps = $xpath->query('//p');
    $i = 0;
    foreach ($ps as $p) 
    {
        if($i != 0) {
            $imgs = $p->getElementsByTagName('img');

            print($imgs->length);
            echo '<br/>';

            if ($imgs->length > 0)
            {
                $p->setAttribute('class', 'image-content');

                foreach ($imgs as $img)
                {
                    $img->removeAttribute('class');
                }
            }
        }
        $i++;
    }

    $htmlFinal = $parse->saveHTML();

    print_r(htmlspecialchars($htmlFinal));             
    echo '<hr/><hr/><hr/>';

    return $htmlFinal;
}

      

Total execution time in seconds: 0.00034999847412109



METHOD 2

The problem is caused LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD

(which makes the first one <p>

as a parent), but you can remove the document tags without that. So, you can do it like here:

function get_parsed_blog_post()
{
$page_content_html = ob_wp_content(false);
$doc = new DOMDocument();
$doc->loadHTML($page_content_html);
foreach($doc->getElementsByTagName('p') as $paragraph) {
    $imgs = $paragraph->getElementsByTagName('img');
    if ($imgs->length > 0)
    {
        $paragraph->setAttribute('class', 'image-content');

        foreach ($imgs as $img)
        {
            $img->removeAttribute('class');
        }
    }        
}


/* REMOVING DOCTYPE, HTML AND BODY TAGS */

// Removing DOCTYPE
$doc->removeChild($doc->doctype);

// Removing HTML tag
$doc->replaceChild($doc->firstChild->firstChild, $doc->firstChild);

// Removing Body Tag
$html = $doc->getElementsByTagName("body")->item(0);
$fragment = $doc->createDocumentFragment();
while ($html->childNodes->length > 0) {
    $fragment->appendChild($html->childNodes->item(0));
}
$html->parentNode->replaceChild($fragment, $html);

$htmlFinal = $doc->saveHTML();

print_r(htmlspecialchars($htmlFinal));             
echo '<hr/><hr/><hr/>';

return $htmlFinal;
}

      

Total execution time in seconds: 0.00026822090148926

+2


source







All Articles