0

PHP fgets function is not reading the following text file lines fully. I'm using fgets to read line by line of some data.

Lines that fail to read fully are as follows;

%107A195U<u--8+5-9+6-17+3-5v<1

truncates to:  %107A195U

%113A120+<l56+2l5-6+<17+2-+6-+2

truncates to: %113A120+

It only reads part of those lines but as soon as it encounters the "<" less than sign, it truncates that line. Other similar lines read fine, but some with the < character in them get truncated. If however I put a single ' quote after the less than symbol then the line reads in full. For example <' results in the whole line being read properly. Very odd. It's like the less than sign is sometimes effecting the fgets line read, depending on what other characters sit next to it.

For example other lines that read perfectly OK are shown below;

%106A191+->2-15l8v<><10-4+u

%108A189+l-E-hj2l6+3l-14+2->-6l4+l2

%109A188vh->Xfp-+8l-3+5-l3l+11-l>7

%110A186+wo2v+o4-2++qd5+5-+<3+<+3-+1

PROGRAM CODE

<?php
echo "START " ."<br>";
$FileName = "./test5.txt";
$infile = fopen($FileName, "r") or die("Unable to open file!");
$dataline = fgets($infile);
while(!feof($infile)) {
  $dataline = fgets($infile);  
  echo " DATALINE ==> " . $dataline . "<br>"; //debug 
}//endloop
?>

PROGRAM DATA FILE - text5.txt

TEST DATA FILE
%106A191+->2-15l8v<><10-4+u 
%107A195U<u--8+5-9+6-17+3-5v<1
%108A189+l-E-hj2l6+3l-14+2->-6l4+l2
%109A188vh->Xfp-+8l-3+5-l3l+11-l>7
%110A186+wo2v+o4-2++qd5+5-+<3+<+3-+1
%111A186qGBl4l<+<3+3l-5+u15u6
%112A182+u>2-4-2+2-16+2u+-2+-3->2
%113A120+<l56+2l5-6+<17+2-+6-+2
%114A120l>2-50+->u2g+<18+-6+<69+
END DATA

PROGRAM OUTPUT

START 
DATALINE ==> %106A191+->2-15l8v<><10-4+u 
DATALINE ==> %107A195U DATALINE ==> %108A189+l-E-hj2l6+3l-14+2->-6l4+l2 
DATALINE ==> %109A188vh->Xfp-+8l-3+5-l3l+11-l>7 
DATALINE ==> %110A186+wo2v+o4-2++qd5+5-+<3+<+3-+1 
DATALINE ==> %111A186qGBl4l<+<3+3l-5+u15u6 
DATALINE ==> %112A182+u>2-4-2+2-16+2u+-2+-3->2 
DATALINE ==> %113A120+ DATALINE ==> %114A120l>2-50+->u2g+<18+-6+<69+ 
DATALINE ==> END DATA

Seeking explanation and solution.

007
  • 21
  • 4
  • 4
    Let me guess, you're viewing this through a web browser.... try using "view source" – Mark Baker Aug 07 '15 at 11:05
  • 3
    View as plain text, and it will be fine. If you need to view it in a browser, then use `htmlentities()` to convert the `<` characters into HTML code that will be displayed properly in the browser. – Simba Aug 07 '15 at 11:18
  • Yes I'm viewing the output in a web browser. I will follow up on the comments and report back. Thanks. – 007 Aug 07 '15 at 11:42
  • Ahh that's odd. View Source shows the whole line properly. So somehow the browser is interpreting the '<' output characters as html code I gather. So I will try that htmlentities() funtion and report back. – 007 Aug 07 '15 at 11:47
  • Folks yes that htmlentities() function solved this problem. Thank you very much for your responses. Much appreciated. SOLUTION: echo " DATALINE ==> " . htmlentities($dataline) . "
    ";
    – 007 Aug 07 '15 at 11:51

1 Answers1

1

Folks yes that htmlentities() function solved this problem. Thank you very much for your responses. Much appreciated.

SOLUTION:

echo " DATALINE ==> " . htmlentities($dataline) . "<br>";
007
  • 21
  • 4