Jump to content



Photo

[php] how to br2nl ?


  • Please log in to reply
6 replies to this topic

#1 barryman

barryman

    Zaara..

  • Joined: 08-November 03
  • Location: Den Haag

Posted 27 September 2005 - 15:44

Hi guys,

I allready know how to use nl2br. So if I insert a new record in a database, all the linebreaks are stored as <br />

But now, I want to remove all of the <br /> tags out of the text field when I want to update a record.

Example with br tags:
Posted Image

What I want:
Posted Image


I've tried php.net for a br2nl code, I got these 2 scripts:

As stated in the manual above, PHP's nl2br() feature only puts a "<br />" tag before each newline ("\n"). So -- if you intend to code a br2nl() function for yourselves, all you have to do is remove every occurence of "<br />" or "<br>".

Rather than get into nasty regular expressions to accomplish this, just use what PHP has built in already --  str_replace():

<?php
   /* br2nl for use with HTML forms, etc. */
   function br2nl($text)
   {
       /* Remove XHTML linebreak tags. */
       $text = str_replace("<br />","",$text);
       /* Remove HTML 4.01 linebreak tags. */
       $text = str_replace("<br>","",$text);
       /* Return the result. */
       return $text;
   }
?>

The final result from this function being called is whatever was entered before XHTML/HTML linebreaks were added.

All newlines are preserved by default, as per PHP ln2br() specification. Since the code above preserves newlines also, you can expect your data to reappear in the same way it was entered.

Hope this helps.

and:

A note to add to the br2nl. Since nl2br doesn't remove the line breaks when adding in the <br /> tags, it is necessary to strip those off before you convert all of the tags, otherwise you will get double spacing. Here is the modified function:


function br2nl($str) {
   $str = preg_replace("/(\r\n|\n|\r)/", "", $str);
   return preg_replace("=<br */?>=i", "\n", $str);
}


The code I need to transfer into text without <br /> tags is:

<?php echo $row_rs_k['klein']; ?>

So how am I supposed to combine these codes??? I've tried to replace $row_rs_k to some places, but it didn't work out, I didn't get a result in the update fields..

Anyone?

Edited by barryman, 27 September 2005 - 15:51.



#2 sjobbe

sjobbe

    Neowinian

  • Joined: 11-July 05
  • Location: Finland

Posted 27 September 2005 - 15:48

Dont use nl2br before you insert to the dbase... Use it when you read from the dbase

#3 OP barryman

barryman

    Zaara..

  • Joined: 08-November 03
  • Location: Den Haag

Posted 27 September 2005 - 15:52

like how then?

#4 KingDrew

KingDrew

    D Tizzy

  • Joined: 14-January 05
  • Location: Paris, Texas

Posted 27 September 2005 - 15:51

$string = str_replace("<br />","",$string);


#5 GatorV

GatorV

    Internet Surfer

  • Joined: 22-May 04
  • Location: /home/mx/gdl/

Posted 27 September 2005 - 15:51

store your text as it's submitted, then when displaying the data use nl2br to convert \n to <br>, that way your database data is stored as you submitted it.

#6 OP barryman

barryman

    Zaara..

  • Joined: 08-November 03
  • Location: Den Haag

Posted 27 September 2005 - 15:57

crap, it worked :s didn't know you could do that...

But now I have to convert all of the pages where I've used nl2br..

Sooo, anyone an idea on how to do the br2nl anyways??

#7 OP barryman

barryman

    Zaara..

  • Joined: 08-November 03
  • Location: Den Haag

Posted 27 September 2005 - 16:02

fok it :D

i'm gonna use nl2br to display the texts and not with inserting it

yeah baby! :p