简体   繁体   中英

Problems after upgrading to PHP 5.2.6

I am in a predicament here. My PHP code used to work but since I upgraded to PHP 5.2.6 I now get:

Warning: domdocument::domdocument() expects parameter 2 to be long, string given in C:\xamps older\xampp167\htdocs\test\myphp\CSR Demo pack\csr\output.php on line 46

Fatal error: Call to undefined method domdocument::loadHTML() in C:\xamps older\xampp167\htdocs\test\myphp\CSR Demo pack\csr\output.php on line 50

Here is the source:

<html>
    <head>
    </head>

    <body>

        <?php

        //error_reporting(0);

        /********** outputer **********/

        $bool=file_exists("data/tcmpsr.txt");
        if ($bool == 1)
        {
            $numstr=file_get_contents("data/tcmpsr.txt");
            if ($numstr <> "")
            {
                $NumOfTitleChars= $numstr;
            }
        }
        Else
        {

        }

        $bool=file_exists("data/dcmpsr.txt");
        if ($bool == 1)
        {
            $numstr=file_get_contents("data/dcmpsr.txt");
            if ($numstr <> "")
            {
             $NumOfDtlChars = $numstr;
            }
        }
        Else
        {

        }

        /*** A new DOM object ***/
            $dom = new DOMDocument('1.0','utf-8');

            /*** load the html into the object ***/
            $html = file_get_contents("data/csr.html");
            $dom->loadHTML($html);

            /*** discard white space ***/
            $dom->preserveWhiteSpace = false;

            /*** get the tags by its tag name ***/
            $artclArray = $dom->getElementsByTagName('div');
            $hdArray = $dom->getElementsByTagName('h2');
            $spanArray = $dom->getElementsByTagName('span');

            $i=0;
            $x=0;
            $y=0;
            $z=0;

        foreach ($artclArray as $memberid)
        {
            $divArray[$x]=$memberid->nodeValue;
            $x++;
        }

        foreach ($hdArray as $memberid)
        {
            $hdlsArray[$y]=$memberid->nodeValue;
            $y++;
        }

        foreach ($spanArray as $memberid)
        {
            $dtlsArray[$z]=$memberid->nodeValue;
            $z++;
        }

        $hdsele=0;
        foreach($hdlsArray as $elemid)
        {
            $AmountOfHdlChars=$NumOfTitleChars;
            $eachTitle=$elemid;

            for ($count=0; $count<$AmountOfHdlChars; $count++)
            {
                $chara[$count]=$eachTitle[$count];
            }
            $smallTitle=implode($chara);
            $shortTitleArray[$hdsele]=$smallTitle;
            $hdsele++;
        }

        $dtlele=0;
        foreach($dtlsArray as $elemid)
        {
            $AmountOfDtlChars=$NumOfDtlChars;
            $eachDetail=$elemid;

            for ($count=0; $count<$AmountOfDtlChars; $count++)
            {
                $chara[$count]=$eachDetail[$count];
            }
            $smallDtl=implode($chara);
            $shortDtlArray[$dtlele]=$smallDtl;
            $dtlele++;
        }
        //echo "".$shortTitleArray[0]."<br />";
        //echo "".$shortDtlArray[0]."<br />";
        //echo "".$divArray[0]."<br />";

        //The Result is three arrays "shortTitleArray", "divArray" and //"shortDtlArray".
        //hdlsArray and dtlsArray contain full actual headlines and details.


        /******************************/

        ?>

        </body>
</html>

After doing some research online I found that Zend extensions could be the cause. Here is what my Zend setup looks like.

This program makes use of the Zend Scripting Language Engine: Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies with the ionCube PHP Loader v3.1.34, Copyright (c) 2002-2009, by ionCube Ltd., and with Zend Extension Manager v1.2.0, Copyright (c) 2003-2007, by Zend Technologies with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies

I have tested this code on several new Windows systems and it has failed on all. This leads me to believe that most, if not many, webhosts might be using a similar setup to the one I have. The common solution, based on what I have read, is to disable Zend or comment out specific areas of the php.ini file. Because this is part of a script meant for distribution, most of my customers will not have direct access to the php.ini nor have access rights to modify webserver settings such as Zend extensions.

How do I solve this in code? Is there a workaround? Or is there an alternative to using DOMDocument ?

You could try SimpleXML , as an alternative to DOMDocument. It should work out-of-the-box on all PHP5 installations.

For example, this should be pretty close to what your DOMDocument code was meant to do:

<?php
// Load the string and create a SimpleXMLElement
$html_string = file_get_contents("data/csr.html");
$root = new SimpleXMLElement($html_string);

// Fetch all div, h2 and span values
$divs = $h2s = $spans = array();
foreach ($xml->div as $div) {
    $divs[] = $div;
}
foreach ($xml->h2 as $h2) {
    $h2s[] = $h2;
}
foreach ($xml->span as $span) {
    $spans[] = $span;
}

// etc...
?>

Haven't tested it tho, so you may have to adapt it a bit.

There are also a number of other alternatives to parse XML documents (and most valid HTML documents, by extension). See PHP: XML Manipulation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM