Page 1 of 1

nl2br in reverse? replace<br \>with line breaks in tex

Posted: Sun Nov 14, 2004 3:31 pm
by sebnewyork
Hi all

I've used nl2br to replace line breaks in a text area with <br /> before.
Now I'm trying to do just the opposite, and can't figure out how.

Basically I want to echo in a text field some text from an html file.

I tried the following code, using str_replace:

<?php
$fp = fopen ("text.htm", 'r');
$data = str_replace("<br />", "\n\r", $fp);
echo "$data";
?>

but this doesn't work.
Could you help me?
?>

Posted: Sun Nov 14, 2004 3:39 pm
by Ambush Commander
What you've done is tried to replace the <br /> from the file handle.

Can't do that, unfortunantly. Try print_r($fp) and you'll see why.

After you get the file handle, then you have to open the file. Try adding this code after your fopen line.

Code: Select all

$fp = fread($fp,filesize('text.htm');
//note that you're overwriting the file handle. You might want to 
//change the variable of the file handle.

Posted: Sun Nov 14, 2004 5:38 pm
by sebnewyork
thanks, but in the meantime I solved my problem another way: here's the code I used, which works perfect -didn't even need to use 'fopen()':

Code: Select all

<?php
$original = file_get_contents ("text.htm");
$search = '<br />';
$replace = '';
$new_content = str_replace ($search, $replace, $original);
echo "$new_content";
?>
I left the $replace empty (didn't replace with '\n\r') because somehow the line breaks where already preserved from the htl to the textarea...

So this works