Link to home
Start Free TrialLog in
Avatar of itnifl
itniflFlag for Norway

asked on

Reading and displaying tabs in PHP

I read and display a file this way:

<some code here>
while(!feof($file)) {
$number++;
$result .= "<tr><td width=\"11\">$number.</td><td>". fgets($file) ."</td></tr>";
}
<some code here to finish the html table>
echo $result;

The problem is that when the table is displayed, tabs are not displayed. Thereby, the structure of the text, if tabs are used, is not correct.
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

By tab you mean the whitespace \t ? Your function fgets($file) gets only the string values so reading the file:
"This is     just a text."
will get [this] [is] [just] [a] [text] (without a tab after "is").
Avatar of Shinesh Premrajan
Hope this helps
$result ='<table "width=100%">';
while(!feof($file)) {
$number++;
$result .= "<tr><td width=\"11\">$number.</td><td>". fgets($file) ."</td></tr>";
}
$result ="</table>";

echo $result;

Open in new window

Avatar of itnifl

ASKER

Raods Roads: I need to display also the tabs. How would I do that?
shinug: you replaced my pseudocode(if you can call it that), with real code. That was not my intention :)
Can you post a sample of that text file ?
Try either of these, as the text file need to be binary safe, b and t tags cane be used for such cases.

$handle = @fopen("test.txt", "rb");

$handle = fopen("test.txt", "rt");

Hope this helps
SOLUTION
Avatar of Shinesh Premrajan
Shinesh Premrajan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itnifl

ASKER

Worked like a charm. Thanks!
FYI,  

str_replace("\t",  "&nbsp;&nbsp;&nbsp;", $text);

wont pickup the \t in your text it .. :-)

Avatar of itnifl

ASKER

The line I ended using was:
$result .= "<tr><td width=\"11\">$number.</td><td>". str_replace("\t",  "&nbsp;&nbsp;&nbsp;", fgets($file)) ."</td></tr>";

It worked with me.
Why did you suspect that it wouldn't work? Just curious..