Solved

Reading and displaying tabs in PHP

Posted on 2011-02-23
10
294 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you post a sample of that text file ?
0
 
LVL 17

Expert Comment

by:shinuq
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 17

Assisted Solution

by:shinuq
shinuq earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Worked like a charm. Thanks!
0
 
LVL 17

Expert Comment

by:shinuq
Comment Utility
FYI,  

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

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

0
 
LVL 2

Author Comment

by:itnifl
Comment Utility
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.
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 count occurrences of each item in an array.

744 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

17 Experts available now in Live!

Get 1:1 Help Now