Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    I've tried to search for this, I guess fairly simple problem, but haven't been able to find a solution.

    Two part question:
    If I use a simple text editor, such as Notepad, to write a few paragraphs and then try to retrieve and display it (through the use of PHP)in a column defined by a table with absolute horizontal limits, I'm confronted with two options:


    1 either I include a "preformatted text" tag like:
    "echo ("<pre>$text</pre>");
    which leads to an output that exceeds the horizontal limits set in
    the table definition. (same problem with <div> tags)Or I leave out
    the <pre> tags and get an output that now is contained within the
    H-limits defined by the table, but now lacks formatting like line-
    breaks and paragraph spacing.

    2 If I try to use a "*.doc" file as a source file, I get an output
    containing all sorts of odd symbols as a replacement for
    formatting code. How can I fix that?

    I've tried with the following two code variants:
    PHP Code:
    <?php 
        $filename 
    "some_catalog/file.txt";
        
    $filesize filesize($filename);
        
    $file fopen$filename"r" );
        
    $text fread$file$filesize );
        
    fclose$file ); 

        echo(
    "<pre>$text</pre>");
    ?>
    and:

    PHP Code:
    $fp = @fopen ('somefile.txt''r'
    or die (
    'Cannot open somefile.txt'); 

    while (
    $line = @fgets ($fp1024)) 

    print 
    $line


    @
    fclose ($fp
    or die (
    'Cannot close somefile.txt'); 
    ?> 
    S. Rosland

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Try this:
    PHP Code:
    $text str_replace("/n""<br>"$text);
    $text str_replace(" ""&nbsp;"$text); 
    That will make $text retain the formatting it has in the text file.

  4. #3
    Member Black Vivi's Avatar
    Join Date
    Apr 2003
    Location
    UK
    Posts
    75
    Member #
    1072
    it'd be better to use this:
    PHP Code:
    $text nl2br($text); 
    Download Firebird now!

  5. #4
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Thanks again for quick reply!

    I tried both methods and inserted the new $text-variable definition prior to the 'echo' command. I also removed the <pre></pre> tags from the echo-statement.

    Black Vivi , your method achieved my goal! :classic: The text output retains its original formatting with regard to paragraphs, and at the same time wraps nicely within the bounderies defined by the table.

    Transio, I tried the same method with your suggestion, but then all pre-formatting disappears. (maybe I misunderstood or placed the definition in the wrong place)

    [hr]
    Rosland
    S. Rosland

  6. #5
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    The best way to retain text formating is to use correct mime type using the header( ) function:

    <?php
    $filename = "some_catalog/file.txt";
    $filesize = filesize($filename);
    $file = fopen( $filename, "r" );
    $text = fread( $file, $filesize );
    fclose( $file );

    header("Content-type: text/plain");
    echo $text;
    ?>
    There and Back Again :Ogre:


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:41 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com