Porting PHP5 to legacy PHP4, DOMDocument quibbles

2019-08-26 07:38发布

问题:

I'm trying to make some of my php5 code work on a legacy server, which unfortunately cannot be upgraded (client's machine).

if (!isset($docRoot)) {
    $docRoot = $_SERVER['DOCUMENT_ROOT'];
}

// generic storage class for the words/phrases
$t = new stdClass();
$t->lang = $curPage->lang;



// load xml translations, could split this into different files..
$translations = new DOMDocument();
$translations->load($docRoot."/xml/translations.xml");

$words = $translations->getElementsByTagName("word");
$count = 0;
foreach( $words as $word )
{

    $name = $word->getAttribute('name');
    $trans = $word->childNodes;

    if ($trans->length > 0) {
        for($i = 0; $i < $trans->length; $i++) {
            $child = $trans->item($i); 



            if ($child->nodeName == $curPage->lang) {
                $t->$name = $child->nodeValue;
            }


        }
    }

}

I've got as far as working out that domdocument is missing a ton of methods in php4 (it's php4.4.4, on a centos box), some of which seem to be replaced by global static functions.. .domxml_open_file()? The XML also has an encoding of UTF8, and the site is in ISO-8859-1..

Bottom line here is, I'm lost! How to make this stuff work on legacy php4? Are there any gotchas about using unicode on php4..?

Thanks.

回答1:

Well, I managed to get this going - fortunately, I found on ister.org a backport of php5's simpleXML functions, which work just fine on php4.4. I've rewritten a lot of the code, and it wasn't too tricky.

Hope this helps someone else in the future.