Newsgroup: it.comp.www.html
Titolo: Help with Formatting Text.

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 

 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

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.


 [/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

?>
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.


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.


 $contents = preg_replace("\t","<IMG SRC=spacer.gif>",$contents);
For replacing the tabs, str_replace() would be quicker and less resource
hungry.


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*


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

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.


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.
