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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

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

Post 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?
?>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

Post 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
Post Reply