Help with Formatting Text.

Discussion in 'HTML' started by Chuck, Nov 27, 2003.

  1. Chuck

    Chuck Guest

    Hello,

    I like to use PHP 'readfile' command and CCS classes to separate
    design from content and style. Simply I place a 'readfile' command
    within a CSS class, and have it read a text file to the page.

    It makes updating text a bit easier. However, unless I use the 'PRE'
    tag the text will not come out formatted, and I need to use the 'br'
    to make the line breaks.

    However, when I use 'PRE' the text will not wrap. I was wondering if
    anyone had any ideas on how to get the text to wrap, and still
    preserve the whitespace.

    -Chuck. (www.wormspeaker.com)
    _____________________________________________________
    Spread love and understanding...
    but don't be afraid to bloody your knuckles doing it.
    -Alex Ross
    Chuck, Nov 27, 2003
    #1
    1. Advertising

  2. Chuck <> wrote:
    > I like to use PHP 'readfile' command and CCS classes to separate
    > design from content and style. Simply I place a 'readfile' command
    > within a CSS class, and have it read a text file to the page.


    Are you trying to include a CSS file into every page? That's done in other
    ways. See http://www.w3schools.com/css/css_howto.asp for ways to include
    CSS.
    --
    Michael Wilcox
    mjwilco at yahoo dot com
    Essential Tools for the Web Developer - http://mikewilcox.t35.com
    Michael Wilcox, Nov 27, 2003
    #2
    1. Advertising

  3. Chuck

    Chuck Guest

    >Chuck <> wrote:
    >> I like to use PHP 'readfile' command and CCS classes to separate
    >> design from content and style. Simply I place a 'readfile' command
    >> within a CSS class, and have it read a text file to the page.

    >
    >Are you trying to include a CSS file into every page? That's done in other
    >ways. See http://www.w3schools.com/css/css_howto.asp for ways to include
    >CSS.


    No, I'm including a text file as the text body of a page. So for
    example if I have a news ticker, for example. I don't have to edit the
    HTML page, just edit the text file.

    For example. (the > and < replaced by ] and [, so as not to cause
    problems with those who have HTML sensitive newsreaders.)

    [PRE CLASS=phosp-green]
    [?php readfile("ticker.txt"); ?]
    [/PRE]

    This places the contents of the ticker.txt in the HTML page with color
    and font conforming to the phosp-green class, and formatting following
    the PRE tag. So it has the white space, but the text does not wrap. So
    as long as I carefully set up the line breaks, it's fine, but I'd
    really like the text to wrap.

    I guess you could set up a PHP script to parse the text file and place
    in line breaks every 80 characters or so. However that's way to
    complicated to be used every page. I was hoping for a simpler
    solution, I was just overlooking.

    -Chuck. (www.wormspeaker.com)
    _____________________________________________________
    Spread love and understanding...
    but don't be afraid to bloody your knuckles doing it.
    -Alex Ross
    Chuck, Nov 27, 2003
    #3
  4. Chuck wrote:

    > [PRE CLASS=phosp-green]
    > [?php readfile("ticker.txt"); ?]
    > [/PRE]


    Try:

    <?php

    // get contents of file.
    $contents = file_get_contents("ticker.txt");

    // replace double line breaks with "</p><p>".
    $contents = preg_replace("/\r?\n\r?\n/","</p>\n\n<p>",$contents);

    // output contents, surrounded by <p> and </p>.
    echo "<p>$contents</p>";

    ?>

    Should work OK.

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
    Toby A Inkster, Nov 27, 2003
    #4
  5. Chuck

    Chuck Guest

    ><?php
    >
    > // get contents of file.
    > $contents = file_get_contents("ticker.txt");
    >
    > // replace double line breaks with "</p><p>".
    > $contents = preg_replace("/\r?\n\r?\n/","</p>\n\n<p>",$contents);
    >
    > // output contents, surrounded by <p> and </p>.
    > echo "<p>$contents</p>";
    >
    >?>


    Brilliant. I don't know why I didn't think of it before.

    Now what I need to do is replace tabs in the .txt file with <IMG
    SRC=spacer.gif> So now I need to research how to refer to the tab in
    PHP.

    Off to research.

    Thank you very much.

    -Chuck. (www.wormspeaker.com)
    _____________________________________________________
    Spread love and understanding...
    but don't be afraid to bloody your knuckles doing it.
    -Alex Ross
    Chuck, Nov 28, 2003
    #5
  6. Chuck

    Chuck Guest

    This looks should work I think.

    // replace tabs with "<IMG SRC=spacer.gif>".
    $contents = preg_replace("\t","<IMG SRC=spacer.gif>",$contents);

    Off to test.

    Thanks again.

    -Chuck. (www.wormspeaker.com)
    _____________________________________________________
    Spread love and understanding...
    but don't be afraid to bloody your knuckles doing it.
    -Alex Ross
    Chuck, Nov 28, 2003
    #6
  7. Chuck <> wrote:
    > This looks should work I think.
    >
    > // replace tabs with "<IMG SRC=spacer.gif>".
    > $contents = preg_replace("\t","<IMG SRC=spacer.gif>",$contents);


    For replacing the tabs, str_replace() would be quicker and less resource
    hungry.
    --
    Michael Wilcox
    mjwilco at yahoo dot com
    Essential Tools for the Web Developer - http://mikewilcox.t35.com
    Michael Wilcox, Nov 28, 2003
    #7
  8. Chuck

    Chuck Guest

    The following works very well.

    <?php

    // get contents of file.
    $contents = file_get_contents("whatever.txt");

    // replaces line breaks with "<BR>".
    $contents = preg_replace("/\r?\n/","<BR>\n",$contents);

    // replaces tabs with "<IMG SRC=tab.gif>".
    $contents = preg_replace("/\t/","<IMG SRC=spacer.gif>",$contents);

    // output contents, surrounded by <p> and </p>.
    echo "<p>$contents</p>";

    ?>

    Thanks again Toby, I've been wracking my brain for months on this.
    I've been going about it wrong. I've been trying to get <PRE> text
    wrap, rather than just pulling the formatting out of the text file and
    making it HTML readable.

    *Smacks Head*

    -Chuck. (www.wormspeaker.com)
    _____________________________________________________
    Spread love and understanding...
    but don't be afraid to bloody your knuckles doing it.
    -Alex Ross
    Chuck, Nov 28, 2003
    #8
  9. Chuck wrote:

    > Now what I need to do is replace tabs in the .txt file with <IMG
    > SRC=spacer.gif> So now I need to research how to refer to the tab in
    > PHP.


    Better to replace with something like &nbsp;&nbsp;&nbsp;&nbsp;.

    And tabs are "\t".

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
    Toby A Inkster, Nov 28, 2003
    #9
  10. Chuck

    Chuck Guest

    >For replacing the tabs, str_replace() would be quicker and less resource
    >hungry.


    Thanks for the help. preg_replace() was burying my server on the
    longer and more complex pages. As a matter of fact I went ahead and
    replaced the whole thing with str_replace() for speed.

    -Chuck. (www.wormspeaker.com)
    _____________________________________________________
    Spread love and understanding...
    but don't be afraid to bloody your knuckles doing it.
    -Alex Ross
    Chuck, Nov 28, 2003
    #10
  11. Chuck

    Chuck Guest

    >> Now what I need to do is replace tabs in the .txt file with <IMG
    >> SRC=spacer.gif> So now I need to research how to refer to the tab in
    >> PHP.

    >
    >Better to replace with something like &nbsp;&nbsp;&nbsp;&nbsp;.
    >
    >And tabs are "\t".


    Thanks. You're right, the spaces do work better than the spacer
    images. (Look better if the image fails to load too.)

    The only lacking thing is that tabbed tables don't work, but you can't
    have everything, and I can live without them.

    Thanks again for pointing me in the right direction.

    -Chuck. (www.wormspeaker.com)
    _____________________________________________________
    Spread love and understanding...
    but don't be afraid to bloody your knuckles doing it.
    -Alex Ross
    Chuck, Nov 28, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Winanjaya Amidjojo

    Re: formatting text in textbox while typing

    Winanjaya Amidjojo, Jul 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    419
    Winanjaya Amidjojo
    Jul 3, 2003
  2. ald
    Replies:
    0
    Views:
    339
  3. avnrao
    Replies:
    1
    Views:
    666
    =?Utf-8?B?dG9ueQ==?=
    May 13, 2004
  4. Swanand Mokashi
    Replies:
    0
    Views:
    604
    Swanand Mokashi
    May 13, 2004
  5. Mothra
    Replies:
    4
    Views:
    179
    Mothra
    Dec 9, 2003
Loading...

Share This Page