Solved

Reading and displaying tabs in PHP

Posted on 2011-02-23
10
338 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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 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: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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

630 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