php - Use DOMDocument with ob_start breaks my HTML code

I'm trying to use a PHP function with DOMDocument for substitute some line in my final HTML code using Wordpress.Th

I'm trying to use a PHP function with DOMDocument for substitute some line in my final HTML code using Wordpress.

The function seems to be fine, but it breaks my HTML code in some points.

i.e. original code:

            document.addEventListener( 'DOMContentLoaded', function() {
                jQuery && jQuery( function( $ ) {
                    // insert placeholder for events injected when a product is added to the cart through AJAX
                    $( document.body ).append( '<div class=\"wc-facebook-pixel-event-placeholder\"></div>' );
                } );
            }, false );

Changed code:

    document.addEventListener( 'DOMContentLoaded', function() {
        jQuery & jQuery( function( $ ) {
            // insert placeholder for events injected when a product is added to the cart through AJAX
            $( document.body ).append( '<div class=\"wc-facebook-pixel-event-placeholder\">' );
        } );
    }, false );

In this case has changed the '&&' with '&' in the JS function. But there are other glitches like this, like space added or removed. It put also some "" tag randomly in the page. This arbitrary are making my page not working fine, as they breaks codes used by my theme.

This is my complete code:

function fix_iubenda_pixelWoocommerce_obStart() { ob_start("fix_iubenda_pixelWoocommerce"); }
function fix_iubenda_pixelWoocommerce_obStop() { ob_end_flush(); }
add_action('wp_head', 'fix_iubenda_pixelWoocommerce_obStart');
add_action('wp_foot', 'fix_iubenda_pixelWoocommerce_obStop');

function fix_iubenda_pixelWoocommerce( $buffer ) {
        $doc = new \DOMDocument;
        $doc->preserveWhiteSpace = 1;
        $doc->strictErrorChecking = 0;
        $doc->formatOutput=0;
        libxml_use_internal_errors(true);

        //$doc->loadHTML($buffer);
        $encoded = mb_convert_encoding( $buffer, 'HTML-ENTITIES', 'UTF-8' );
        $ok = $doc->loadHTML( $encoded, LIBXML_HTML_NODEFDTD | LIBXML_NOBLANKS );
        if ( !$ok ) {
           return $content;
        }
        $xpath = new \DOMXPath($doc);
        $nodeList = $xpath->query('//script[contains(., "fbq(")]');
        foreach ($nodeList as $node) {
                $node->nodeValue = $node->nodeValue ;
                $node->setAttribute('type', 'text/plain');
                $node->setAttribute('class', '_iub_cs_activate-inline');
        }

        $buffer = $doc->saveHTML();
        return $buffer;
}

It seems that's not related to my manipulation, but there are something in $doc->saveHTML() that breaks the HTML code.

Any suggestion on how to change my code in order to make this script works fine is very welcome.

If you have also solution different than ob_start for change my HTML output are also welcome. I need to to a "search & replace" in all the code, including header and footer, not only the content.

Thanks

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744508972a4577875.html

相关推荐

  • php - Use DOMDocument with ob_start breaks my HTML code

    I'm trying to use a PHP function with DOMDocument for substitute some line in my final HTML code using Wordpress.Th

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信