Heim > Fragen und Antworten > Hauptteil
Ich versuche, einige Skripte, die unter PHP 5.4 laufen, unter PHP 7.4 laufen zu lassen. Es wurden keine Syntaxfehler erkannt, mit Ausnahme eines veralteten Fehlers bei der Referenzübergabe, der behoben wurde.
In einigen Teilen der Anwendung erhalte ich nicht den korrekten Rückgabewert. Ich habe php7cc und PHPStorm ausgeführt, um zu sehen, ob ich Fehler und Verfallswarnungen erkennen konnte, habe aber keine Hinweise darauf erhalten, wo das Problem liegen könnte.
Es gibt ein Skript, das die XML-Daten analysiert und in ein Klassenobjekt einfügt. _MV_WRITE_._MV_SEP_._MV_SEP_.$this->mvKey._MV_SEP_.$this->mvObj->toString()._MV_SEP_Dieser Teil ist korrekt, außer dass $item->toString() ein Leerzeichen anstelle der richtigen Zeichenfolge zurückgibt Wert.
Gibt es etwas in diesem Code, das in PHP 5.4 funktioniert, aber nicht in PHP 7.4? Unten finden Sie den betreffenden Code.
Hauptskript
<html> <head> <title>Demo MV</title> </head> <body> <?php include_once 'demo_mv.php'; include_once 'demo_xml_parser.php'; $xmltext = "<Test></Test>"; echo "Creating XML Parser. <br>"; $xp = New XMLParser(); echo "Parsing XML to MV item. <br>"; If( !$xp->ParseToMVItem( $xmltext ) ) { echo "Parsing XML item failed. <br>"; } echo "Getting formatted output. <br>"; $item = $xp->GetMVItem(); echo print_r( $item ) . "<br>"; $fv = New MVFile("123456789", $item); $mvObject = $fv->getFormattedItem( ); echo "Formatted output: " . $mvObject . "<br>"; ?> </body> </html>
XML-Parser
<?php Class XMLParser { // set up some variables for use by the parser var $xp; // Parser var $g_level = 0; // Current XML depth var $g_amc = 0; // Current Result index var $currentTag = ""; // Name of current element var $g_elements = Array(); // Names of elements by depth var $g_result = Array(); // Result array var $errstring; // String of error message var $MVItem; // MV Item object To Parse into var $currentData = ""; // Data in current element Function XMLParser() { // Constructor } Function ParseToMVItem( $xml ) { // Initialise MV Item $this->MVItem = New MVItem(""); // create parser $this->xp = xml_parser_create(); xml_set_object( $this->xp, $this ); // set element handlers xml_set_element_handler($this->xp, "elementBegin", "elementEnd"); xml_set_character_data_handler($this->xp, "characterData"); xml_parser_set_option($this->xp, XML_OPTION_CASE_FOLDING, FALSE); // parse data if (!xml_parse($this->xp, $xml, True )) { $errstring = " Line: " . xml_get_current_line_number( $this->xp); $errstring .= " Column: " . xml_get_current_column_number( $this->xp) . "<br/>"; $errstring .= xml_error_string(xml_get_error_code($this->xp) ); $this->errstring = "XML parser error: " . $errstring ; // destroy parser xml_parser_free($this->xp); Return False; } // Success // destroy parser xml_parser_free($this->xp); Return True; } Function elementBegin($xp, $name, $attributes) { //file_log( "Attributes: " . $attributes . "rn" ); // opening tag handler $this->g_amc++; $amc = $this->g_amc; $level = $this->g_level ++; //file_log( "Level: " . $level . "rn" ); $this->g_elements[ $level -1 ] = $name; // export the name of the current tag to the global scope $this->currentTag = $name; $this->currentData = ""; // Add to MV Item $s_level = $level; settype( $s_level, "string"); $this->MVItem->replace( $s_level, $amc, 1 ); $this->MVItem->replace( $name, $amc, 2 ); $ename = $this->MVItem->extract( $amc , 2 ); // Handle attributes $attrib = ""; $index = 0; foreach ($attributes as $key => $value) { $index ++; $attrib = trim( $key . "=" . $value ); // echo $attrib . " - index=$index<br/>"; $this->MVItem->replace( $attrib, $amc, 3, $index ); // echo $this->MVItem->extract( $amc, 3, $index ) . "=attrib$index<br/>"; } } Function elementEnd($xp, $name) { // closing tag handler // echo $this->currentData; $this->MVItem->replace( trim($this->currentData), $this->g_amc, 4 ); $this->g_level --; $this->currentTag = $this->g_elements[ $this->g_level -1] ; } Function characterData($xp, $data) { // character data handler $data = Trim( $data ); If($data != '') { // echo $data . "<br />"; $this->currentData .= " ". $data; // $this->MVItem->replace( $data, $this->g_amc, 4 ); } } Function GetMVItem() { Return $this->MVItem; } } ?>
MVItem
<?php if ( ! defined( '_MV_CLASS' ) ) { define('_MV_CLASS', true ); /* * Define constants */ define('_MV_LOGIN_',1); define('_MV_LOGOFF_',2); define('_MV_EXECUTE_',3); define('_MV_CALL_',4); define('_MV_SELECT_',5); define('_MV_OPEN_',6); define('_MV_READNEXT_',7); define('_MV_READ_',8); define('_MV_READU_',9); define('_MV_WRITE_',10); define('_MV_RELEASE_',11); define('_MV_DELETE_',12); define('_MV_CLOSE_',13); define('_MV_TEST_EXIST_',14); define('_MV_LOCK_ITEM_',15); define('_MV_READV_',16); define('_MV_NOOP_',18); define('_MV_SELECT_TCL_',17); define('_MV_STATUS_',19); define('_MV_SEP_',chr(001)); define('_MV_AM_',chr(254)); define('_MV_VM_',chr(253)); define('_MV_SVM_',chr(252)); define('_MV_DIFFDATE_',732); define('_MV_DIFFTIME_',86400000); define('_MV_CONNECTION_TCP_',1); define('_MV_CONNECTION_TCP_PROXY_',2); class MVFile{ // Private var $mvKey; var $mvObj; function __construct($key, $item){ $this->mvKey = $key; $this->mvObj = $item; } function getFormattedItem(){ $this->mvKey = _MV_WRITE_._MV_SEP_._MV_SEP_.$this->mvKey._MV_SEP_.$this->mvObj->toString()._MV_SEP_; return $this->mvKey; } } // MVFile class MVItem { var $Record; var $origrec; function __construct($rec = ""){ if(empty($rec)){ $this->Record = Array(); $this->origrec = $this->Record; return; } $this->setRecord($rec); } function setRecord($newval){ $rec = explode(_MV_AM_,$newval); $nbam = count($rec); $this->Record = Array(); $this->origrec = $newval; for($am = 0;$am < $nbam;$am++) { $vam = explode(_MV_VM_,$rec[$am]); $nbvm = count($vam); $lvm = Array(); for($vm = 0;$vm < $nbvm;$vm++){ $vvm = explode(_MV_SVM_,$vam[$vm]); $lvm[$vm] = $vvm; } $this->Record[$am] = $lvm; } return true; } function toString(){ return $this->extract(); } function extract($am = 0,$vm = 0,$svm = 0) { $am --; $vm --; $svm --; //file_log( "SVM: " . $svm . "rn" ); if($vm == -1) $svm = -1; if($am == -1) { $nbam = count($this->Record); $res = ""; for($am = 0;$am<$nbam;$am++) { $nbvm = count($this->Record[$am]); $lvm = Array(); for($vm= 0;$vm<$nbvm;$vm++){ if(is_array($this->Record[$am][$vm])){ $lvm[$vm] = implode(_MV_SVM_,$this->Record[$am][$vm]); }else{ $lvm[$vm] = $this->Record[$am][$vm]; } } if(is_array($lvm) ){ $res[$am] = implode(_MV_VM_,$lvm); } else { $res[$am] = $lvm; } } if(is_array($res)) { return implode(_MV_AM_,$res); } else { return $res; } } if($vm == -1) { $nbvm = count($this->Record[$am]); $lvm = Array(); for($vm= 0;$vm<$nbvm;$vm++){ $lvm[$vm] = implode(_MV_SVM_,$this->Record[$am][$vm]); } return implode(_MV_VM_,$lvm); } if($svm == -1 ){ // MM fix undefined offset if(count($this->Record) < ( $am + 1 ) ) { //file_log("SVM = -1 rn"); return ""; } if(count($this->Record[$am]) < ( $vm + 1 ) ) { //file_log("SVM = -1 rn"); return ""; } // End Of fix return implode(_MV_SVM_,$this->Record[$am][$vm]); } else { return $this->Record[$am][$vm][$svm]; } } function replace($newval,$am = 0, $vm = 0 , $svm = 0){ if($am == 0) return; if($vm == 0) $svm = 0; for($i=count($this->Record);$i < $am;$i++){ $this->Record[$i]= Array(""); } if($vm == 0) { $this->Record[$am-1] = array($newval); }else{ for($i = count($this->Record[$am-1]);$i < $vm ; $i++){ $this->Record[$am-1][$i] = Array(""); } if($svm == 0) { $this->Record[$am-1][$vm-1] = Array($newval); }else{ for($i = count($this->Record[$am-1][$vm-1]);$i < $svm ; $i++){ $this->Record[$am-1][$vm-1][$i] = Array(""); } // $this->Record[$am-1][$vm-1][$svm-1] = Array($newval); $this->Record[$am-1][$vm-1][$svm-1] = $newval; } } } function ins(&$tab,$pos,$newval){ $fin = $tab; $nb = count($tab); array_splice($tab,-($nb-$pos),($nb-$pos)); array_splice($tab,-1,1,array($newval,"")); array_splice($fin,0,($pos-1)); array_splice($tab,-1,1,$fin); } function insert($newval, $am = 0,$vm = 0,$svm = 0){ if($am == 0) return; if($vm == 0) $svm = 0; if ($am > count($this->Record) ) { $this->replace($newval,$am); return; } if($vm == 0) { $this->ins($this->Record,$am,array($newval)); }else{ if($vm > count($this->Record[$am-1])) { $this->replace($newval,$am,$vm); return; } if($svm == 0) { $this->ins($this->Record[$am-1],$vm,$newval); }else{ for($i = count($this->Record[$am-1][$vm-1]);$i < $svm -1 ; $i++){ $this->Record[$am-1][$vm-1][$i] = Array(""); } $this->ins($this->Record[$am-1][$vm-1],$svm,$newval); } } } function delete($am = 0 ,$vm = 0,$svm = 0) { if($am == 0) return; if($vm == 0) $svm =0; if($vm == 0){ array_splice($this->Record,$am - 1,1); }else{ if($svm == 0) { array_splice($this->Record[$am-1],$vm - 1,1); }else{ array_splice($this->Record[$am-1][$vm-1],$svm - 1,1); } } } function AMCount(){ //MM //echo '<' . count( $this->Record ) . '>'; return count($this->Record); } function VMCount($am){ if ($am > count($this->Record) || $am == 0) return 0; $tmp = $this->Record[$am-1]; return count($tmp); } function SVMCount($am,$vm){ if ($am > count($this->Record) || $am == 0) return 0; $tmp = $this->Record[$am-1]; if ($vm > count($tmp) || $vm == 0 ) return 0; $tmp = $tmp[$vm-1]; return count($tmp); } } // MVItem } ?>
Das Folgende sind die korrekten Rückgabewerte in PHP 5.4
Creating XML Parser. Parsing XML to MV item. Getting formatted output. MVItem Object ( [Record] => Array ( [0] => Array ( [0] => Array ( [0] => 0 ) [1] => Array ( [0] => Test ) [2] => Array ( [0] => ) [3] => Array ( [0] => ) ) ) [origrec] => Array ( ) ) 1 Formatted output: 101234567890�Test��
Hier sind die Ergebnisse, die ich in PHP 7.4 erhalte:
Creating XML Parser. Parsing XML to MV item. Getting formatted output. MVItem Object ( [Record] => Array ( [0] => Array ( [0] => Array ( [0] => 0 ) [1] => Array ( [0] => Test ) [2] => Array ( [0] => ) [3] => Array ( [0] => ) ) ) [origrec] => Array ( ) ) 1 Formatted output: 101234567890
Der XML-Parser sollte in Ordnung sein, da das zurückgegebene Array dasselbe ist. Das eigentliche Problem liegt in der Methode getFormattedItem(), die eigentlich nur die Methode extract() umfasst. Ich habe versucht, an verschiedenen Stellen Druckanweisungen hinzuzufügen, kann aber scheinbar nicht feststellen, welche Anweisung das Problem verursacht.
P粉4425761652023-07-18 09:21:50
只需查看您的最终输出。我猜可能是字符集的问题。通常,新安装的 PHP 版本的 default_charset 值是 UTF-8,但旧版本可能将该值设置为 ISO-8859-1(即 latin-1),因此请检查您的 php.ini(以及可能更改字符集的项目中的其他脚本)。