Reply LinkBack Thread Tools Search this Thread
Old 01-04-2004, 17:05   #1 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
Running str_replace on a fread txt file

<php-newbie-alert />

As the title implies, I'm trying to run a str_replace on an included/read document.

Basically, I'm trying to import the contents of a .txt file and convert \n to <br /> tags.


I can do the whole fopen …*fclose sweep just fine.
Before the fclose, I declare the contents (string) of the file as a variable.

I'm then attempting the run the variable through a str_replace process.

It will work fine if I remove the str_replace part.
It will work fine (w/ the str_replace) if the variable string doesn't originate from an fread document.
I just can't seem to get the two to work together.

Here's what I have.

Code:
<?php $filename = "bios/martinenorman.txt"; $file = fopen($filename, "r"); $filesize = filesize($filename); $text = fread($file, $filesize); fclose($file); $text = str_replace("\n",'<br />', $text); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>str_replace</title> </head> <body> <p> <?php echo($text); ?> </p> </body> </html>

The str_replace is just being ignored.

Help?

TIA
  Reply With Quote
Old 01-04-2004, 17:20   #2 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
Scrap that.
Found this.

Code:
$text = nl2br($text);

Presumed it would give me <br> instead of <br />.
Pleasantly surprised to find…
  Reply With Quote
Old 01-04-2004, 17:50   #3 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
Scrap all that, found that Tex-Edit Plus was to blame.
Silly arse was using non-standard invisible characters to indicate new lines.

Swapped to another text app and went with ereg_replace() instead.

:happybunny:
  Reply With Quote
Old 01-04-2004, 18:15   #4 (permalink)
gray
i still want paying
 
gray's Avatar
 
Join Date: Oct 2003
Location: newcastle, uk
Posts: 4,728
glad we could help mate
  Reply With Quote
Old 02-04-2004, 05:42   #5 (permalink)
cam
vague™
 
cam's Avatar
 
Join Date: Mar 2004
Location: Glasgow
Posts: 5,484
lol gray

and bill, nl2br is so handy, php is loaded with great little functions like that
__________________
  Reply With Quote
Old 02-04-2004, 05:54   #6 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
I ended up using this:
Code:
$text = ereg_replace("\r","",$text); $text = ereg_replace("\n","<br />\n",$text); $text = ereg_replace(".<br />","\n</p>\n<p>",$text);

It enables me to format the contents of the txt file in a way that matches the surrounding markup (à la view source).

nl2br() is great, but a little too basic for my (pedantic) needs.


I'm wearing my PHP is fun! badge today.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8