Archived

This topic is now archived and is closed to further replies.

[php] how to br2nl ?

Recommended Posts

barryman    0

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:

br1.jpg

What I want:

br2.jpg

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 "&lt;br /&gt;" 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 "&lt;br /&gt;" or "&lt;br&gt;".

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

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

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 &lt;br /&gt; 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("=&lt;br */?&gt;=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?

Share this post


Link to post
Share on other sites
sjobbe    0

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

Share this post


Link to post
Share on other sites
KingDrew    0
$string = str_replace("&lt;br /&gt;","",$string);

Share this post


Link to post
Share on other sites
GatorV    1

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.

Share this post


Link to post
Share on other sites
barryman    0

like how then?

Share this post


Link to post
Share on other sites
barryman    0

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??

Share this post


Link to post
Share on other sites
barryman    0

fok it :D

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

yeah baby! :p

Share this post


Link to post
Share on other sites

  • Recently Browsing   0 members

    No registered users viewing this page.