处理PHP数组时截断结果

时间:2018-08-23 18:44:55

标签: php

随着操作系统的更新,运行了多年的PHP脚本突然出现了我无法弄清的截断错误。它是由一个不再有空的人写的,所以我所知道的不多。

该脚本处理文本文件以提取标题,然后构建带有链接的编号列表以创建新页面以显示相关的简短文章和照片。

文本文件包含以下条目:

#22:New Channel 
AD:One mile north of Old Harbor 
DT:1905-06 
PN:NewCut1905.jpg 
PT:Hydraulic Dredge Works on New Harbor Channel
TX:Because the Kalamazoo River did not have enough water discharge to scour a clear channel through the tremen....

在html列表中,以上条目显示为链接“ 22.New Channel”,但现在显示为“ 2.N”

下面是整个脚本,但似乎失败了:

$entar[$entCt] = $ar[1];
$entTlar[$entCt] = $ar[2];

整个名称(例如“ New Channel”)在$ ar [2]中,但是$ entTlar [$ entCt]的结尾仅是第一个字母。随附的数字也将被截断为第一个数字,以使21,21,23变为2,2,2

这是现在的整个脚本。我打算清理html,但PHP更重要。

<?php
$file = ''; $tfile = ''; $ln = ''; $entCt = 0; $perCol = 0; $i = 0; $j = 0;
$entar = ''; $entTlar = ''; $s = '';

$file = fopen("entry.txt", 'r');
while(!feof($file)) {
    $ln = fgets($file, 1024);
    if(substr($ln, 0, 1) == '#') { // entry number line

        preg_match('/^.(\d+):(.*)$/', $ln, $ar); // separate entry # & short title
        if(substr($ar[2], 0, 4) == ' ---') continue; // skip unused numbers
        $entar[$entCt] = $ar[1];
        $entTlar[$entCt] = $ar[2];
        ++$entCt;
    }
}
fclose($file);
$perCol = floor(($entCt + 3) / 4); // show entries in 4 columns
//echo "perCol= $perCol<br>";
//$c1 = $c2 = $c3= $c4='';
for($i = 0; $i < $perCol; ++$i) { // 1/4rd of the entries in each column
    $in = $entar[$i]; $it = $entTlar[$i];
    $s .= "<tr><td width=25%><a href=\"entry.php?$in\"><b>$in</b>. $it</a></td>"; // column 1
//$c1.="$i,$in;";
    $in = $entar[$i+$perCol]; $it = $entTlar[$i+$perCol];
    $s .= "<td width=25%><a href=\"entry.php?$in\"><b>$in</b>. $it</a></td>"; // column 2
//$c2.="$i,$in;";
    $j = $i + ($perCol * 2);
    if($j >= $entCt) { $s .= "</tr>\n"; continue; }
    $in = $entar[$j]; $it = $entTlar[$j];
    $s .= "<td width=25%><a href=\"entry.php?$in\"><b>$in</b>. $it</a></td>"; // column 3   
//$c3.="$i,$in;";
    $j = $i + ($perCol * 3);
    if($j >= $entCt) { $s .= "</tr>\n"; continue; }
    $in = $entar[$j]; $it = $entTlar[$j];
    $s .= "<td width=25%><a href=\"entry.php?$in\"><b>$in</b>. $it</a></td></tr>\n"; // column 4
//$c4.="$i,$in;";
}

?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Explore the Map</title>
</head>
<body bgcolor="c0c0d0" marginwidth=20 link="000055" vlink="222266">
<center>

<H1>&#149;&nbsp;&nbsp;Welcome to the Tales of the Villages Album&nbsp;&nbsp;&#149;</H1><H2>Click Here For Tales and Photographs
<a href="mapCt.php">.</a></H2>
<?php
echo "<table width=95% border=1>";
echo "$s";
echo "</table>";
?>
</center>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

我做了一个quick test,它表明从php 7.1.0起,通过数组符号在空字符串上设置字符现在仅设置了该字符(应如此),而在以前的版本中,php会自动强制转换字符串到数组。在脚本的顶部,您有$entar = '';,它正在将$entar初始化为字符串,并在下面用作数组。您可以将其更改为$entar = [];,将其初始化为应有的数组。

注意:这将解决此问题。我没有仔细阅读其余代码,以确保没有其他类似的惊喜。