Help with Formatting Text.

C

Chuck

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
 
M

Michael Wilcox

Chuck said:
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.
 
C

Chuck

Chuck said:
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
 
T

Toby A Inkster

Chuck said:
[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.
 
C

Chuck

<?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
 
C

Chuck

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
 
M

Michael Wilcox

Chuck said:
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.
 
C

Chuck

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
 
T

Toby A Inkster

Chuck said:
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".
 
C

Chuck

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

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
 
C

Chuck

Now what I need to do is replace tabs in the .txt file with <IMG
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top