alexcmm Posted February 6, 2007 Share Posted February 6, 2007 Hello, I'm hoping there's an easy way to turn a <BR> back into something else... preferrably a space. I'm using this code to turn a /n into a <BR> $program=$_POST['program']; $program = preg_replace("/\n/","\n<BR>\n",$program); How can I do the reverse? Can you help? Link to comment https://forums.phpfreaks.com/topic/37320-need-help-with-line-breaks-but-in-reverse/ Share on other sites More sharing options...
Psycho Posted February 6, 2007 Share Posted February 6, 2007 $program = preg_replace("\n<BR>\n","/\n/",$program); Link to comment https://forums.phpfreaks.com/topic/37320-need-help-with-line-breaks-but-in-reverse/#findComment-178386 Share on other sites More sharing options...
alexcmm Posted February 6, 2007 Author Share Posted February 6, 2007 Thanks mjdamato, I am new, but I did think to try that before coming here and it doesn't work. Any more ideas? Link to comment https://forums.phpfreaks.com/topic/37320-need-help-with-line-breaks-but-in-reverse/#findComment-178387 Share on other sites More sharing options...
Balmung-San Posted February 6, 2007 Share Posted February 6, 2007 I can see why that wouldn't work. "\n<BR>\n" isn't a valid regex as far as I'm aware. You probably want this: $program = preg_replace("/<BR>/","\n",$program); Granted, I'm no regex master, but that should work. Link to comment https://forums.phpfreaks.com/topic/37320-need-help-with-line-breaks-but-in-reverse/#findComment-178391 Share on other sites More sharing options...
trq Posted February 6, 2007 Share Posted February 6, 2007 First question. Why are you using.... preg_replace("/\n/","\n<BR>\n",$program); when nl2br exists? Take a look at the man entry for nl2br, I'm sure people have posted a reversal function. Link to comment https://forums.phpfreaks.com/topic/37320-need-help-with-line-breaks-but-in-reverse/#findComment-178392 Share on other sites More sharing options...
kenrbnsn Posted February 6, 2007 Share Posted February 6, 2007 You should be using the function nl2br() to add the <br /> tag before the newline characters "\n". Then to remove them and just leave the newline characters do a <?php $line = str_replace("<br />\n","\n",$line); ?> Ken Link to comment https://forums.phpfreaks.com/topic/37320-need-help-with-line-breaks-but-in-reverse/#findComment-178396 Share on other sites More sharing options...
alexcmm Posted February 6, 2007 Author Share Posted February 6, 2007 Actually, <BR> is is being pulled from a file which I am including in my form, not an actual form field. I finally got it to work with this code: $program=str_replace("<br>","\n", $program); Thanks to all for your help! Link to comment https://forums.phpfreaks.com/topic/37320-need-help-with-line-breaks-but-in-reverse/#findComment-178414 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.