php - 处理 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];
整个名称,例如“新频道”在 $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>• Welcome to the Tales of the Villages Album •</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>
解决方案
我做了一个快速测试,它表明从 php 7.1.0 开始,通过数组表示法在空字符串上设置字符现在只是设置该字符(应该如此),而在以前的版本中,php 会自动将字符串转换为数组。在脚本的顶部,您将$entar = '';
其初始化$entar
为字符串,并在下面用作数组。您可能只需将其更改为$entar = [];
将其初始化为应有的数组即可。
注意:这将解决这一问题。我没有通读其余代码以确保没有其他类似的惊喜。
推荐阅读
- python - Pandas——如果日期在几天之内,则加入两个数据集
- python - 颠倒顺序
- python - 使用 DFS 在图中查找循环 - 递归问题
- c++ - 使用自上而下的方法最大化切割段?
- .htaccess - Aapche2 htaccess RewriteCond 与 ErrorDocument 404
- machine-learning - PyTorch 的 nn.Transformer 中的 src 和 tgt 有什么区别?
- r - R:计算向量中长度N个元素的均值和方差
- data-visualization - 画面自定义调色板..任何人都知道为什么这不起作用
- maven - SoapUI Project 在 Jenkins 传递依赖错误中使用 Maven 插件测试执行
- jersey - Jersey/JAX-RS WriterInterceptor 和 HTTP 状态