RaythMistwalker Posted October 27, 2011 Share Posted October 27, 2011 <?php echo nl2br($PostText); ?> In this instance $PostText is: Welcome to your new forum./n/n This is a default post to show you everything is set up. But it isn't changing the /n to <br>. The database is a varchar(10000) for this required column. Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/ Share on other sites More sharing options...
The Little Guy Posted October 27, 2011 Share Posted October 27, 2011 because it is \n not /n also \n is a string, so that wont be converted. Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282716 Share on other sites More sharing options...
RaythMistwalker Posted October 27, 2011 Author Share Posted October 27, 2011 Ok So how do i get it to work? Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282720 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 27, 2011 Share Posted October 27, 2011 why not use str_replace? Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282721 Share on other sites More sharing options...
The Little Guy Posted October 27, 2011 Share Posted October 27, 2011 why not use str_replace? Because that makes for bad database data. I don't know how your inserting your values, php? phpMyAdmin? Other? Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282724 Share on other sites More sharing options...
ManiacDan Posted October 27, 2011 Share Posted October 27, 2011 Ok there's a couple things here... 1) /n is not a special character. If you have something that's giving you /n instead of an actual newline (\n), you need to fix that. 2) If you already have a lot of /n characters in your database, write a script to str_replace /n for \n 3) Once you fix 1 and perform 2, nl2br will begin working. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282735 Share on other sites More sharing options...
RaythMistwalker Posted October 27, 2011 Author Share Posted October 27, 2011 Ok there's a couple things here... 1) /n is not a special character. If you have something that's giving you /n instead of an actual newline (\n), you need to fix that. 2) If you already have a lot of /n characters in your database, write a script to str_replace /n for \n 3) Once you fix 1 and perform 2, nl2br will begin working. -Dan Ok il answer in order.. 1) That was a mistake by me. I went to the single post that had /n and changed to \n 2 ^ 3) It still doesn't work :/ I can link to the thread if that would help. Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282739 Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2011 Share Posted October 27, 2011 What does "it doesn't work" mean, exactly? Show all of the relevant code where you're trying to echo the data, and an actual sample of the data. Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282754 Share on other sites More sharing options...
RaythMistwalker Posted October 27, 2011 Author Share Posted October 27, 2011 Query & Setting $PostText $PostQuery = "SELECT * FROM ".FORUM_POSTS." WHERE post_id='{$PostID}'"; $PostRes = mysql_query($PostQuery, $db); $PostText = mysql_result($PostRes, 0, 'post_text'); This query works fine and gets all the data perfectly fine. This part shows the text: <td class="row2" valign="top"> <br><?php echo nl2br($PostText); ?><br> </td> What it returns: Welcome to your new forum.\n\n This is a default post to show you everything is set up. An example page is Here Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282764 Share on other sites More sharing options...
The Little Guy Posted October 27, 2011 Share Posted October 27, 2011 when you look in the database do you see \n or a newline? if you see \n, that than is your problem. don't save it in the database as \n, save it as a newline. Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282766 Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2011 Share Posted October 27, 2011 Is there anything else between where the data is retrieved and where it's echoed? Something, somewhere has to be changing the \n newlines into string literal 'backslash n's. How is the data inserted into the database field to begin with? Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282769 Share on other sites More sharing options...
RaythMistwalker Posted October 27, 2011 Author Share Posted October 27, 2011 It's saved as \n in the database. As I said in a previous post I am using varchar(10000) so how would I set it as new line? Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282775 Share on other sites More sharing options...
ManiacDan Posted October 27, 2011 Share Posted October 27, 2011 You are seeing the string \n. The string \n is not a newline, it's the string \n. That's the problem. The Little Guy just said it up there but I wanted to make it clear: $a = "\n"; //$a is now a NEWLINE $a = '\n'; //$a is now THE STRING \n You have the string \n scattered throughout your entries. You need to do my #2 above and replace the string \n with the newline character, commonly represented in interpolated strings as \n. Yes, it's confusing. Welcome to computer science. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282777 Share on other sites More sharing options...
RaythMistwalker Posted October 27, 2011 Author Share Posted October 27, 2011 How do I actually do the newling rather than having string? I'm getting a bit confused. It's only 1 entry that requires to be changed since I only have 1 entry atm. Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282780 Share on other sites More sharing options...
Psycho Posted October 27, 2011 Share Posted October 27, 2011 I assume you are entering the data via PHPMyAdmin. If so, you cannot enter a line break into the input field if you use the Insert Record form. Change the field type to "TEXT" instead of VARCHAR and you should be able to enter line breaks Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282782 Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2011 Share Posted October 27, 2011 You are seeing the string \n. The string \n is not a newline, it's the string \n. That's the problem. The Little Guy just said it up there but I wanted to make it clear: $a = "\n"; //$a is now a NEWLINE $a = '\n'; //$a is now THE STRING \n You have the string \n scattered throughout your entries. You need to do my #2 above and replace the string \n with the newline character, commonly represented in interpolated strings as \n. Yes, it's confusing. Welcome to computer science. -Dan That's what I thought until I just did the following: $str = 'new\nline'; // should be a string literal $query = 'INSERT INTO table (field) VALUES (\'' . $str . '\')'; $result = mysqli_query($dbc, $query); $query = "SELECT field FROM table"; $result = mysqli_query($dbc, $query); $array = mysqli_fetch_row($result); echo $array[0]; echo ' <br><br> '; echo $nl2br($array[0]); The above generates this source: [pre] new line <br><br> new<br /> line [/pre] Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282784 Share on other sites More sharing options...
RaythMistwalker Posted October 27, 2011 Author Share Posted October 27, 2011 TY for everyones help. Changing it to text then entering the new line worked. Quote Link to comment https://forums.phpfreaks.com/topic/249918-nl2br/#findComment-1282785 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.