Jump to content

converting line break to <br> php


MrGarcia

Recommended Posts

i have a new problem now..

when im posting a news.. and submit it..

the text has no LINE BREAK or <BR>

 

example..

i submit this news:

 

------------

Welcome to my site.

i hope you like it..

 

thank you.

admin

------------

 

after submitting it..

this is the result:

 

Welcome to my site. i hope you like it.. thank you. admin

Link to comment
Share on other sites

Try this... as an alternative. Generally nl2br() is the correct term.

<?php
$news = "This is my news.
Hey there!
Thanks buddy! =)
Hi!";

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

echo $news; // Should return the "news" with line breaks.
?>

 

OR

 

<?php
$news = "This is my news.
Hey there!
Thanks Buddy! =)
Hi!";

echo nl2br($news); // Should print it perfectly fine the way you want it.
?>

 

Link to comment
Share on other sites

Try this... as an alternative. Generally nl2br() is the correct term.

<?php
$news = "This is my news.
Hey there!
Thanks buddy! =)
Hi!";

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

echo $news; // Should return the "news" with line breaks.
?>

 

This will only work if his editor used the same line breaks as the user's browser did (in your code) for example:

 

$news = "This is my news.\nHey there!\nThanks buddy! =)\nHi!";//assume from $_POST
echo str_replace("\r\n", "<br />", $news);

Link to comment
Share on other sites

This will only work if his editor used the same line breaks as the user's browser did (in your code) for example:

 

Not to sound, mean, but I use that exact code for my boards on my website. e.g. You posting a reply on here, you hit enter button, it supplies a "<br>"... And it works for every browser, not just some... Which is why I use it...

Link to comment
Share on other sites

This will only work if his editor used the same line breaks as the user's browser did (in your code) for example:

 

Not to sound, mean, but I use that exact code for my boards on my website. e.g. You posting a reply on here, you hit enter button, it supplies a "<br>"... And it works for every browser, not just some... Which is why I use it...

 

Yes on your Windows machine. Now, try that on Linux and Mac and see if that still works :)

Link to comment
Share on other sites

hi there guys..

thank you for your reply..

 

btw. here's the my code when adding news..

 

<?php

include("config.php");

  if(isset($_POST['submit']))
  {//begin of if($submit).
      // Set global variables to easier names
     // and pervent sql injection and apostrophe to break the db.
      $title = mysql_escape_string($_POST['title']);
      $text1 = mysql_escape_string($_POST['text1']);
      $text2 = mysql_escape_string($_POST['text2']);
              //check if (title) field is empty then print error message.
              if(!$title){  //this means If the title is really empty.
                     echo "Error: News title is a required field. Please fill it.";
                     exit(); //exit the script and don't do anything else.
              }// end of if
         //run the query which adds the data gathered from the form into the database
         $result = mysql_query("INSERT INTO news (title, dtime, text1, text2)
                      VALUES ('$title',NOW(),'$text1','$text2')",$connect);
          //print success message.
          echo "<b>Thank you! News added Successfully!<br>";
          echo "<meta http-equiv=Refresh content=2;url=admin.php>";
  }//end of if($submit).
  // If the form has not been submitted, display it!
else
  {//begin of else
      ?>
      <br>
      <form method="post" action="<?php echo $PHP_SELF ?>">
                 <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="17%">Title : </td>
            <td width="83%"><input name="title" type="text" size="30" maxlength="255"></td>
          </tr>
          <tr>
            <td>Brief Message : </td>
            <td><textarea name="text1" cols="35" rows="10"></textarea></td>
          </tr>
          <tr>
            <td>Message : </td>
            <td><textarea name="text2" cols="35" rows="10"></textarea></td>
          </tr>
          <tr>
            <td> </td>
            <td><input type="submit" name="submit" value="Add News"></td>
          </tr>
                  </table>
      </form>
      <?
  }//end of else
?>

 

and this is my code in showing the list of news @ news.php

<?php
include("config.php");

        $result = mysql_query("SELECT * FROM news ORDER BY newsid DESC",$connect);
        //lets make a loop and get all news from the database
        while($myrow = mysql_fetch_assoc($result))
             {//begin of loop
               //now print the results:
               echo "<font color='#0099CC' size='2'><strong>";
               echo $myrow['title'];
               echo "</strong></font><br>";
               echo "<font color='#0066CC' size='1'>On: <i>";
               echo $myrow['dtime'];
               echo "</font>";
               echo "</i><br>";
               echo "<font color='#000066'>";
               echo $myrow['text2'];
               echo "</font>";

               // If Admin logged in (Edit & Delete the news)
             if (session_is_registered('myusername')) {
             echo "<br><a href=\"edit_news.php?newsid=$myrow[newsid]\">Edit</a> | <a href=\"delete_news.php?newsid=$myrow[newsid]\">Delete</a>";
            }
            else
             {
             echo "";
}
               echo "<br><br><div class='dots'> </div>";
             }//end of loop
?>

 

guys.. if you know where to put code..

please repost the code here together with the code i gave..

 

thank you..

Link to comment
Share on other sites

it's your code, you should know what is what.

 

where are you outputting the text that needs the line breaks?  find that, and follow the many examples already posted in this thread.  you gotta give it a try before just asking for people to do it for you.  it's how you learn.

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.