1

Following an OS update, a PHP script that has run well for years has suddenly developed a truncating errors that I can't figure out. It was written by someone who's no longer available, so I don't know any more than I can see.

The script processes a text file to extract titles then build a numbered list with links to create new pages to display the related short articles and a photo.

The text file contains entries such as:

#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....

In the html list, the above entry was appearing as a link reading "22.New Channel" but now displays as "2.N"

The entire script is further below, but where it seems to be failing is on:

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

The entire name, such as "New Channel" is in $ar[2] but $entTlar[$entCt] ends up with nothing more than the first letter. The accompanying numbers are also being truncated to the first numeral so that 21,21,23 become 2,2,2

Here's the entire script as it is now. I intend to clean up the html but the PHP is more critical.

<?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>
6
  • 3
    $entar = ''; is this variable a string or array? Commented Aug 23, 2018 at 18:48
  • 3
    @FelippeDuarte Good catch. A quick test I did shows that older versions of php would have treated the variable as an array while newer (7.1) treats it as a string accessing a single character in a character array. @OP, you can likely just change that variable to something like $entar = []; and it would fix it. No telling what other issues might arise though. This older code could definitely use a refresh. Commented Aug 23, 2018 at 18:54
  • @Jonathan That's terrifying both on the creator's and PHP's part Commented Aug 23, 2018 at 18:59
  • @MonkeyZeus I agree. PHP has pretty much always had single character access for strings via array notation as it is based on C. However, the automatic type conversion to an array when setting a character on an empty string by php is pretty bad. Note, if the string has any value, it works as expected, even when setting values beyond the end of a string. I assume this is why this was corrected for 7.1+. 3v4l.org/aLKWP Commented Aug 23, 2018 at 19:09
  • I would have to agree that it's definitely more "proper" as of PHP 7.1 but the author's decision to declare a string at the beginning and silently convert to an array in the middle of code is probably the ultimate culprit for OP's issue; simply heinous. Commented Aug 23, 2018 at 19:13

1 Answer 1

2

I did a quick test and it shows that as of php 7.1.0, setting a character via array notation on an empty string now just sets that character (as it should) whereas in previous versions php would automatically cast the string to an array. At the top of your script, you have $entar = ''; which is initializing $entar as a string and it is used below as an array. You can likely just change that to say $entar = []; which will initialize it as an array as it should be.

Note: This will fix this one issue. I didn't read through the rest of the code to make sure there are no other surprises like this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.