• 0

[php] how to br2nl ?


Question

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?

Edited by barryman
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

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

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.