Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:35:26+00:00 2026-06-17T05:35:26+00:00

I need to display a plain-text file that contains two-space tab’d columns of data

  • 0

I need to display a plain-text file that contains two-space tab’d columns of data in a web page.

What I did was to use PHP to read the text file and print it out between <pre> tags to use a monospace font as so:

<pre>
<?php
  $fn="data.txt";
  $fi=fopen($fn, "r");
  $fc=fread($fi, filesize($fn));         //open and read text file
  fclose($fi);
  $fc=str_replace("\t", "  ", $fc);      //replace tabs with two spaces
  print($fc);                            //print data between PRE tags
?>
</pre>

It almost works, but the tabs are being troublesome. It is trivial to replace the tabs with two spaces, but then non-whitespace characters are pushed over instead of absorbed into the tabs. True tabs absorb n-1 non-whitespace characters (where n is the number of spaces per tab).

For example, the following table should be displayed as so:

|   | 43| 43|  7|   |   |
| 12|128|128|128|   | 53|
|  3|  3|  3|  3|   |   |
|   |   | 21| 21| 39|   |

However by blindly replacing all tabs with two-spaces, we get this:

|    |  43|  43|    7|   |   |
|  12|128|128|128|   | 53|
|   3|   3|   3|   3|   |   |
|   |   |  21|  21|  39|   |

I’m trying to figure out a (reasonably easy) way to convert the tabs to spaces while accounting for tabs that don’t take up the full n spaces.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:35:27+00:00Added an answer on June 17, 2026 at 5:35 am

    I have written this function some time ago, might be helpful:

    function tab2space($line, $tab = 4, $nbsp = FALSE) {
        while (($t = mb_strpos($line,"\t")) !== FALSE) {
            $preTab = $t?mb_substr($line, 0, $t):'';
            $line = $preTab . str_repeat($nbsp?chr(7):' ', $tab-(mb_strlen($preTab)%$tab)) . mb_substr($line, $t+1);
        }
        return  $nbsp?str_replace($nbsp?chr(7):' ', '&nbsp;', $line):$line;
    }
    

    It was meant to deal with multibyte strings, if you have only numbers, you can get rid of mb_, it will speed up this function.

    [+] Note that this is meant to work with one line, so you will need to process line by line with fgets instead of whole file at once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would need to build a HTML document from plain text and display it
I need to take text from a source as plain text and display it
Ok so I have a text file that will change regularly that I need
I need to convert markdown text to plain text format to display summary in
I need to display short description like label and near it data associated with
I have a page that fires several xmlHttp requests (synchronous, plain-vanilla javascript, I'd love
I need to display large-ish text files (under 10 MB) on mobile device browsers.
I need to select a csv file from local file system and display the
I am creating a web application that allows a user to load data in
I am trying to configure HTMLPurifier to only display external links as plain text.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.