3raser Posted July 5, 2010 Share Posted July 5, 2010 Why isn't $message_spaces making it so $reply has spaces in it? When someone presses enter, it should make spaces in their post. $before = array("\r\n", "\n", "\r"); $after = array("<br/>", "<br/>", "<br/>"); $message_spaces = str_replace($before, $after, $reply); Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/ Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 Why don't you use the nl2br function? <?php $message_spaces = nl2br($reply); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081352 Share on other sites More sharing options...
3raser Posted July 5, 2010 Author Share Posted July 5, 2010 Oh, didn't know it existed. I'll try it. EDIT: It doesn't seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081356 Share on other sites More sharing options...
3raser Posted July 5, 2010 Author Share Posted July 5, 2010 Anyone have any suggestions? I'm not actually typing \n or \nr or \r in my message, I just thought when you press enter, nl2br would automatically pick it up? Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081390 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 We need to see more of your code. The form definition. Where you're displaying the results. Ken Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081397 Share on other sites More sharing options...
Pikachu2000 Posted July 5, 2010 Share Posted July 5, 2010 Perhaps you misspoke about what you're trying to do, but that code wouldn't put spaces in text; it would put in html line breaks. Have you viewed the html source to see if that's happening? Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081405 Share on other sites More sharing options...
3raser Posted July 5, 2010 Author Share Posted July 5, 2010 <?php session_start(); ?> <style> a { color:black; font-weight:bold; } holder { width:600px; } table { width:600px; } </style> <?php $session = $_SESSION['user']; $thread = $_GET['thread']; mysql_connect($mysql_host, $mysql_user, $mysql_password); mysql_select_db($mysql_database); $get_thread_exist = mysql_query("SELECT COUNT(id) FROM topics WHERE id='$id'"); $reply = $_POST['message']; $reply = mysql_real_escape_string($reply); $thread_post = $_POST['hidden']; $ip = $_SERVER['REMOTE_ADDR']; $date = date("M-D-Y"); $get_thread_lock = mysql_query("SELECT locked FROM topics WHERE id='$id'"); $locked = mysql_fetch_assoc($get_thread_lock); $before = array("\r\n", "\n", "\r"); $after = array("<br/>", "<br/>", "<br/>"); $message_spaces = nl2br($reply); if(!$session) { echo "<center><div class='holder'><table border='1'><tr><td><center>Sorry, you must be logged in to access this page! <a href='index.php'>Home</a></center> <br/></td></tr></table></div></center>"; } elseif(!$thread && !$thread_post) { echo "". $locked ."<center><div class='holder'><table border='1'><tr><td><center>You haven't selected a thread to reply to! <a href='index.php'>Home</a></center> <br/></td></tr></table></div></center>"; } elseif(!$get_thread_exist) { echo "<center><div class='holder'><table border='1'><tr><td><center>You are trying to reply to a thread that doesn't exist! <a href='index.php'>Home</a></center> <br/></td></tr></table></div></center>"; } elseif($locked['locked'] == 1) { echo "<center><div class='holder'><table border='1'><tr><td><center>This topic is locked! <a href='viewtopic.php?id=". $id ."'>Back to topic</a></center> <br/></td></tr></table></div></center>"; } elseif(!$reply) { echo "<center><div class='holder'><table border='1'><tr><td><center><form action='reply.php' method='POST'><br/>Message: <br/><textarea name='message' rows='20' cols='35' maxlength='3000'></textarea><br/><br/> <input type='hidden' value='". $thread ."' name='hidden'><input type='submit' value='Submit Post'></form></center> <br/></td></tr></table></div></center>"; } elseif(strlen($reply) >= 15) { mysql_query("INSERT INTO posts VALUES ('', '$ip', '$date', '$session', '$message_spaces', '$thread_post')"); mysql_query("UPDATE users SET postcount = postcount + 1 WHERE username='$session'"); mysql_query("UPDATE topics SET replies = replies + 1 WHERE id='$thread_post'"); echo "<center><div class='holder'><table border='1'><tr><td><center>Your post has been posted! <a href='viewtopic.php?id=". $thread_post ."'>Go back to thread</a>.</center> <br/></td></tr></table></div></center>"; } else { echo "<center><div class='holder'><table border='1'><tr><td><center>Sorry, you need to have at least 15 characters in your post!</center> <br/></td></tr></table></div></center>"; } ?> And I can't view the source, my website host doesn't display the part that shows the messages. Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081408 Share on other sites More sharing options...
Pikachu2000 Posted July 5, 2010 Share Posted July 5, 2010 You lost me there a little bit. If it gets sent to the browser, you should be able to view the html page source via the menu View --> Page Source, or something similar. However, that doesn't really matter at this point. The question I have is: are you trying to replace line feeds/new lines with spaces or html line breaks? Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081413 Share on other sites More sharing options...
3raser Posted July 5, 2010 Author Share Posted July 5, 2010 Ok, here, this what I'm trying to do: When someone is typing in a text field, and they press ENTER - I want it so PHP knows that, and they make it a <br/> tag. If you just do this in the textarea: hey there It would be: hey there Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081420 Share on other sites More sharing options...
Pikachu2000 Posted July 5, 2010 Share Posted July 5, 2010 I think you might be conflating an html line break (<br />) with a space. If you replace \r, \n, or \r\n with <br />'s, it will still display the same in the browser. If I were to type into a <textarea> : I am\n the one\n \n and only.\n Then replace the \n new lines with <br />'s it would still be rendered by the browser as I am the one and only. Unless I'm completely misreading your posts, I think what you want is more along the lines of this to change new lines and CRs to spaces. $before = array("\r\n", "\n", "\r"); $message_spaces = str_replace($before, ' ', $reply); Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081425 Share on other sites More sharing options...
3raser Posted July 5, 2010 Author Share Posted July 5, 2010 No.....I want it so when someone presses enter a new LINE is made..... Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081426 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 I looked at the code you posted and don't see where you're displaying the message. You're storing it in the database, but not displaying it. The nl2br function should be used when displaying the content, not when storing the content. Ken Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081428 Share on other sites More sharing options...
Pikachu2000 Posted July 5, 2010 Share Posted July 5, 2010 No.....I want it so when someone presses enter a new LINE is made..... I wasn't trying to be difficult, but you were saying you wanted one thing, then providing an example of something different. It's always best to clarify what someone actually wants before you try to give an answer . . . Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081461 Share on other sites More sharing options...
3raser Posted July 5, 2010 Author Share Posted July 5, 2010 I looked at the code you posted and don't see where you're displaying the message. You're storing it in the database, but not displaying it. The nl2br function should be used when displaying the content, not when storing the content. Ken So I should switch back to my original code? And why doesn't it work either? Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081646 Share on other sites More sharing options...
3raser Posted July 6, 2010 Author Share Posted July 6, 2010 Bump. Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081662 Share on other sites More sharing options...
exally Posted July 6, 2010 Share Posted July 6, 2010 like everyone else said, nl2br --- if you have something that addslashes when going into the database you made need to do a str_replace("\\\\", "\\", $string); Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081715 Share on other sites More sharing options...
premiso Posted July 6, 2010 Share Posted July 6, 2010 Have you checked to see if magic_quotes are on? If they are, they automatically run addslashes on any form data. It is suggested to turn them off / disable them (see the documentation on alternatives to this). Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1081717 Share on other sites More sharing options...
3raser Posted July 7, 2010 Author Share Posted July 7, 2010 like everyone else said, nl2br --- if you have something that addslashes when going into the database you made need to do a str_replace("\\\\", "\\", $string); I have mysql_real_escape_string....so I guess that counts? Would it effect my security? Quote Link to comment https://forums.phpfreaks.com/topic/206778-not-replacing-reply/#findComment-1082260 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.