pndof12006 Posted October 16, 2006 Share Posted October 16, 2006 Ok, I am starting a poetry website and when users submit a new poem they currently need to add a [code]<br>[/code] at the end of each line. is it possible to make it so when the form is submitted it automatically puts the breaks in the databse? Link to comment https://forums.phpfreaks.com/topic/24067-automatic/ Share on other sites More sharing options...
trillion Posted October 16, 2006 Share Posted October 16, 2006 something like this maybe:[code]$var = $_POST[poem]."<br/>";[/code]then put $var into the database insert query Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109337 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Author Share Posted October 16, 2006 but that will not put the [code]<br>[/code]at the end of each line of a poem. Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109338 Share on other sites More sharing options...
akitchin Posted October 16, 2006 Share Posted October 16, 2006 look up nl2br() in the php manual. Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109340 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Author Share Posted October 16, 2006 that doesn't help me, from what it looks like u have to have a \n in order for that to work. Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109346 Share on other sites More sharing options...
akitchin Posted October 16, 2006 Share Posted October 16, 2006 .. a newline character, you mean? because that's EXACTLY what hitting enter in a text field does. why don't you actually try using it before claiming it won't work? Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109349 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Author Share Posted October 16, 2006 well the thing is, when it's submitted and inserted into the DB there is no characters that are /n. Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109350 Share on other sites More sharing options...
akitchin Posted October 16, 2006 Share Posted October 16, 2006 you won't see them because they get converted into the actual linebreaks you see.[code]echo nl2br($value_pulled_from_db);[/code]try that. Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109353 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Author Share Posted October 16, 2006 didn't work. Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109355 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Author Share Posted October 16, 2006 when it gets inserted in the DB it is in the same format it ws in the text area. exampleSAMPLESAMPLEwill show like that in the Text Area and in The DB, But when echoed to a page it is like this:SAMPLE SAMPLE Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109356 Share on other sites More sharing options...
akitchin Posted October 16, 2006 Share Posted October 16, 2006 then let's see:a) the MySQL select query that grabs the value from the databaseb) the PHP code that echoes nl2br() of that value Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109357 Share on other sites More sharing options...
Daniel0 Posted October 16, 2006 Share Posted October 16, 2006 pndof12006: Let me explain how it works:\n (LF - line feed) - Linux uses this to display a new line\r (CR - carriage return) - Macintosh use this to display a new line\n\r (CRLF - carriage return, line feed) - Windows uses this to display a new line[url=http://php.net]nl2br[/url] converts LF to [tt][nobbc]<br />[/nobbc][/tt]. If you want to convert all types of new lines to \n, then use this: [code]$string = preg_replace("/(\r\n|\r|\n)/","\n",$string);$string = nl2br($string);[/code]Or perhaps even better: [code]$string = preg_replace("/(\r\n|\r|\n)/","<br />\n",$string);[/code] Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109358 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Author Share Posted October 16, 2006 Daniel it didn't work. Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109360 Share on other sites More sharing options...
Daniel0 Posted October 16, 2006 Share Posted October 16, 2006 Try to post the script that posts the poem. Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109361 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Author Share Posted October 16, 2006 [code]$p = preg_replace("/(\r\n|\r|\n)/","\n",$poem);echo "<tr><td align='center'><b>$pname</b></td></tr>";echo "<tr><td> </td></tr>";echo "<tr><td align='center'>";echo $p;echo "</td></tr>";echo "<tr><td> </td></tr>";[/code] Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109362 Share on other sites More sharing options...
akitchin Posted October 16, 2006 Share Posted October 16, 2006 you forgot the additional nl2br() if you're going to go with his first suggestion.[code]echo nl2br($p);[/code] Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109363 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Author Share Posted October 16, 2006 still doesnt do nething Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109364 Share on other sites More sharing options...
akitchin Posted October 16, 2006 Share Posted October 16, 2006 try this:[code]echo "<tr><td align='center'><b>$pname</b></td></tr>";echo "<tr><td> </td></tr>";echo "<tr><td align='center'>";echo nl2br($poem);echo "</td></tr>";echo "<tr><td> </td></tr>";[/code] Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109366 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Author Share Posted October 16, 2006 doesn't do nething, still shows SAMPLE SAMPLE Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109367 Share on other sites More sharing options...
akitchin Posted October 16, 2006 Share Posted October 16, 2006 alright, give these a shot then:[code]$p = preg_replace("/(\r\n|\r|\n)/","<br />\n",$poem);echo $p;[/code][code]$p = preg_replace("/(\\r\\n|\\r|\\n)/","<br />\n",$poem);echo $p;[/code] Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109369 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Author Share Posted October 16, 2006 alright, finally... It Works... Thanks Guys... Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109371 Share on other sites More sharing options...
xsist10 Posted October 16, 2006 Share Posted October 16, 2006 You'll probably find that his code strips \n before inserting it into the database or that his database stores line breaks as \r only. Link to comment https://forums.phpfreaks.com/topic/24067-automatic/#findComment-109372 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.