我正在尝试将在PHP 5.4上运行的某些脚本在PHP 7.4上运行。除了已经修复的过时的传引用错误之外,没有检测到语法错误。
在应用程序的某个部分,我没有得到正确的返回值。我运行了php7cc和PHPStorm来查看是否可以捕捉到任何错误和过时警告,但没有任何能够解决问题的线索。
有一个脚本解析XML数据并将其放入一个类对象中。_MV_WRITE_._MV_SEP_._MV_SEP_.$this->mvKey._MV_SEP_.$this->mvObj->toString()._MV_SEP_这一部分都是正确的,除了$item->toString()返回的是空白而不是正确的字符串值。
在这些代码中是否有任何在PHP 5.4中有效但在PHP 7.4中无效的内容?以下是有问题的代码。
主要脚本
Demo MV
";
echo "Creating XML Parser.
";
$xp = New XMLParser();
echo "Parsing XML to MV item.
";
If( !$xp->ParseToMVItem( $xmltext ) ) {
echo "Parsing XML item failed.
";
}
echo "Getting formatted output.
";
$item = $xp->GetMVItem();
echo print_r( $item ) . "
";
$fv = New MVFile("123456789", $item);
$mvObject = $fv->getFormattedItem( );
echo "Formatted output: " . $mvObject . "
";
?>
XML Parser
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) . "
";
$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
";
$this->MVItem->replace( $attrib, $amc, 3, $index );
// echo $this->MVItem->extract( $amc, 3, $index ) . "=attrib$index
";
}
}
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 . "
";
$this->currentData .= " ". $data;
// $this->MVItem->replace( $data, $this->g_amc, 4 );
}
}
Function GetMVItem()
{
Return $this->MVItem;
}
}
?>
MVItem
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
}
?>
以下是在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��
以下是我在PHP 7.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
XML解析器应该是没问题的,因为返回的数组是相同的。真正的问题在于getFormattedItem()方法,它实际上只是涉及到extract()方法。已经尝试在各个位置添加打印语句,但似乎无法确定哪个语句导致了问题。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
只需查看您的最终输出。我猜可能是字符集的问题。通常,新安装的 PHP 版本的 default_charset 值是 UTF-8,但旧版本可能将该值设置为 ISO-8859-1(即 latin-1),因此请检查您的 php.ini(以及可能更改字符集的项目中的其他脚本)。