Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading and displaying tabs in PHP

Posted on 2011-02-23
10
Medium Priority
?
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.

719 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