Solved

Reading and displaying tabs in PHP

Posted on 2011-02-23
10
307 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:itnifl
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34967764
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").
0
 
LVL 17

Expert Comment

by:shinuq
ID: 34967890
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

0
 
LVL 2

Author Comment

by:itnifl
ID: 34968064
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 :)
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34968082
Can you post a sample of that text file ?
0
 
LVL 17

Expert Comment

by:shinuq
ID: 34968127
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Assisted Solution

by:shinuq
shinuq earned 150 total points
ID: 34968192
The tab can be seen in the View source of the page, it wont be visible in the browser.For the browser to display the tabs you need to wrap the txt in <pre> tags.

Hope this helps
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 200 total points
ID: 34968393
Tabs are whitespace and browsers collapse multiple whitespace down to a single space. This leaves you 3 options

1. Use <pre></pre> tags as outlined by shinuq above
2. Replace each tab with non-breaking spaces using str_replace   .... str_replace("\t",  "&nbsp;&nbsp;&nbsp;", $text);
3. Use table cells or CSS styling to simulate the tabs.

No.s 1 or 2 are probably the simplest to use especially if you use a monospaced font
0
 
LVL 2

Author Closing Comment

by:itnifl
ID: 34968589
Worked like a charm. Thanks!
0
 
LVL 17

Expert Comment

by:shinuq
ID: 34968787
FYI,  

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

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

0
 
LVL 2

Author Comment

by:itnifl
ID: 34968933
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..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now