Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Break to New Line

Break to New Line

Change a break into a new line.

I was recently chatting with an online buddy Cody and he asked how to achieve the reverse of the php function nl2br() so I showed him. At the same time I figured others could benefit from it so here is the function with an explanation.

Here is the function that uses preg_replace() to make the switch

function breakToNewLine($s, $useReturn=false) {
  return preg_replace('/(<br ?\/?>)/i', (($useReturn) ? '\r' : '\n'), $s);

The second parameter is used to determine whether or not a return should be used instead of a line feed.

The regular expression checks for the opening of a break tag "<", then the actual letters "br". At this point we might find a space, but we might not so we use a "?" which means it could match. Next we look for a possible / and escape this so the regex doesn't error out. Again we may or may not find a "/", so again we use the "?". We look for a closing to the break ">". The last thing to do is add an i modifier for a case insensitive search.

Nothing more to it really, a fairly simple function

Here is a sample

function breakToNewLine($s, $useReturn=false) {
  return preg_replace('/(<br ?\/?>)/i', (($useReturn) ? '\r' : '\n'), $s);

$string = "Hello, <br /> This is a test message.<br>With various 
versions of a break character.<BR>Here is some more random text.<Br /*gt; Finally one more.";
print breakToNewLine($string);

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Break to New Line

 Thomas (iplayguitar214)  Mon Oct 23, 2006 8:52 pm  
Haha, very nice. Honestly havn't found a personal use for it, but i do like it. perhaps one day i will need it.
 Cody Fisher  Thu Nov 23, 2006 12:30 am  
I'm using it for the admin section of my website. I have a contact form set up, and I have nl2br functioning on the user side, so when I go in to reply I like it to look nice.

I have it set up so I can reply to users who contact me, and I use this function so that the body of the message looks good and normal on my end.
 php manual  Sat Jan 27, 2007 7:56 am  
good article, thanks
 mkeefe  Sun Feb 25, 2007 12:28 am  
@Thomas, No problem you may use it at some point.
@Cody, figured you would use it. haha
@php_manual, intersting name and thank you.
 eXodus  Mon Jun 18, 2007 11:51 am  
Hmm Wouldnt this work...
[code] function br2nl($string){
$string = str_replace("
", "", $string);
return $string;

 mikembley  Wed Aug 15, 2007 2:48 pm  
It would indeed work eXodus, for the majority of
tags that are found, But just rembember that not everyone uses XHTML.



Thus this is were preg_replace comes into place. As we do not know how the user intends to spell a Break tag.

By the way Mkeefe. The link to Codys site is broken, Just notifying you.
 baonam (Vietnamese)  Tue Sep 16, 2008 2:59 am  
at first, i want to say thanks very much because of your function, i have an idea, i hope you happy, and function will be better! just idea, nothing else
i tested your function but it doesnt work well, and i changed something like this:
return preg_replace( '/()/i', 'n', $string )
 baonam (Vietnamese)  Tue Sep 16, 2008 3:01 am  
i dont know why my code isnt displayed well, I post it again
[code]preg_replace( '/()/i', 'n', $string );[code]
 Guest  Tue Sep 16, 2008 3:08 am  
i dont know why?
[code]preg_replace( "'"/"("<""/"?"br"(""s"?""w"?")"+""/"?">)"/i'", '""n"'", $string );[code]
i dont know how to post a code, so I insert ", if you want to use my code, you can omit it, all of them:(
 baonam (Vietnamese)  Tue Sep 16, 2008 3:10 am  
happy day!
i forgot to write my name above, i'm sorry :D
 baonam (Vietnamese)  Tue Sep 16, 2008 3:14 am  
[code]preg_replace( "'\"/"("<""/"?"br"("\"s"?"\"w"?")"+"\"/"?">)"/i'", '"\"n"'", $string );[code]

i wish that i know how to post a code or a picture :(
Add a comment
The ability to add new comments has been disabled in this archived version.

Main | Tutorials | Articles