MrGarcia Posted April 20, 2010 Share Posted April 20, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/ Share on other sites More sharing options...
SaMike Posted April 20, 2010 Share Posted April 20, 2010 Run it trough nl2br function before printing <?php $string = "This is multi-line string!"; $string = nl2br($string); echo $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045190 Share on other sites More sharing options...
MrGarcia Posted April 20, 2010 Author Share Posted April 20, 2010 that's not what i mean.. This is what i mean...look @ my screenshot.. unfortunately, after submitting the news.. this is the result.. anyone ?? Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045201 Share on other sites More sharing options...
Alex Posted April 20, 2010 Share Posted April 20, 2010 You were already given the answer. Run nl2br on the data before outputting it. Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045203 Share on other sites More sharing options...
MrGarcia Posted April 20, 2010 Author Share Posted April 20, 2010 where should i place that ?? Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045204 Share on other sites More sharing options...
dotMoe Posted April 20, 2010 Share Posted April 20, 2010 On the page that displays what you wrote in your textbox. Are you saving the textarea data in a database? Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045205 Share on other sites More sharing options...
Alex Posted April 20, 2010 Share Posted April 20, 2010 I already said, use it on the text before outputting. Given you haven't supplied us with any code I can't be more specific. Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045206 Share on other sites More sharing options...
havenpets Posted April 20, 2010 Share Posted April 20, 2010 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. ?> Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045270 Share on other sites More sharing options...
ignace Posted April 20, 2010 Share Posted April 20, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045281 Share on other sites More sharing options...
havenpets Posted April 20, 2010 Share Posted April 20, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045316 Share on other sites More sharing options...
ignace Posted April 20, 2010 Share Posted April 20, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045348 Share on other sites More sharing options...
MrGarcia Posted April 21, 2010 Author Share Posted April 21, 2010 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.. Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045515 Share on other sites More sharing options...
MrGarcia Posted April 21, 2010 Author Share Posted April 21, 2010 i dont know where to put the right code.. Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045526 Share on other sites More sharing options...
mrMarcus Posted April 21, 2010 Share Posted April 21, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045535 Share on other sites More sharing options...
MrGarcia Posted April 21, 2010 Author Share Posted April 21, 2010 thank you guys.. i did it.. thank you. Quote Link to comment https://forums.phpfreaks.com/topic/199144-converting-line-break-to-php/#findComment-1045544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.