Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading and displaying tabs in PHP

Posted on 2011-02-23
10
Medium Priority
?
345 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:Shinesh Premrajan
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 27

Expert Comment

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

Expert Comment

by:Shinesh Premrajan
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
 
LVL 17

Assisted Solution

by:Shinesh Premrajan
Shinesh Premrajan earned 600 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 800 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:Shinesh Premrajan
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

971 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