Jump to content

nl2br


supanoob

Recommended Posts

ok so i have the following function:

 

<?php
function news_item ($news) { $news=htmlspecialchars($news); $news=str_replace (array('[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]'), 

array('<b>', '</b>', '<i>', '</i>', '<u>', '</u>'), $news); return nl2br($news); }
?>

 

but for some reason the nl2br doesnt conver my new lines to <br> when i use it. What i want to do is submit the data from a text area into the database, but when its inserted i want the new lines to stay in place, so when it is echo'd it comes up like:

 

Line 1

 

Line 2

 

Line 3

 

instead of:

 

Line 1 Line 2 Line 3

 

any ideas why?

Link to comment
Share on other sites

aside from the horrible formatting, everything looks fine. Can you show an example of the form maybe, or an example of some text that would be there?

 

sure this is the form used:

 

<form id=\"form1\" name=\"form1\" method=\"post\" action=\"index.php?step=admin&action=news&new=yes\">
  <table width=\"100%\" border=\"0\">
  <tr>
    <td>Title</td>
    <td><input type=\"text\" name=\"title\" id=\"textfield\" /></td>
  </tr>
  <tr>
    <td>Post</td>
    <td><textarea name=\"news\" id=\"textarea\" cols=\"45\" rows=\"5\"></textarea></td>
  </tr>
  <tr>
    <td colspan=\"2\"><center><input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" /></center></td>
    </tr>
</table>

</form>

 

and this is the php side of things:

 

<?php
if ($_GET['new'] == 'yes')
          {
          $title = $_POST['title'];
          $news = $_POST['news'];
    
          $title = stripslashes($title);
          $news = stripslashes($news);
          
          $title = mysql_real_escape_string($title);
          $news = mysql_real_escape_string($news);
          $news = news_item($news);
          
          mysql_query("INSERT INTO news
          (news_title, news_date, news_by, news_message) VALUES
          ('$title', NOW(), '$id', '$news') ") 
          or die(mysql_error()); 
          
          echo "New news item added.</td>
  </tr>
</table>";
?>

 

 

Link to comment
Share on other sites

A couple comments.

 

1. Have you checked the values in the database to see if the BR tags are there?

 

2. You are doing this all wrong. You should save the user's original input to the database without modification (except for mysql_real_escape_string()). You would then modify the data when displaying it on screen according to how it is being displayed. If you are displaying in "one the page" then you would want to use nl2br() and possibly something like htmlentities(). But, if you want the user to edit the data, then you would use it to populate a textarea without those modifications.

 

Just a thought, but does the text in the textarea have actual line breaks OR are you considering the word wrapping, due to the width of the teatarea, as line breaks.

Link to comment
Share on other sites

A couple comments.

 

1. Have you checked the values in the database to see if the BR tags are there?

 

2. You are doing this all wrong. You should save the user's original input to the database without modification (except for mysql_real_escape_string()). You would then modify the data when displaying it on screen according to how it is being displayed. If you are displaying in "one the page" then you would want to use nl2br() and possibly something like htmlentities(). But, if you want the user to edit the data, then you would use it to populate a textarea without those modifications.

 

Just a thought, but does the text in the textarea have actual line breaks OR are you considering the word wrapping, due to the width of the teatarea, as line breaks.

 

so i would nl2br the text before showing it on the page? so it would be $news = nl2br($news); echo $news?

 

and yes the textarea does have actual line breaks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.