DJTim666 Posted August 18, 2007 Share Posted August 18, 2007 I need to know why the following code isn't working for me. It is supposed to take enter's and insert them into the DB as < br / > Code: <?php <tr><td width='80%'><textarea cols='40' rows='4' name='p_message'></textarea></td></tr> $p_message = $_POST['p_message']; $replaceit = array(BAD WORDS =O!); $replaceit2 = array("*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****", "*****","*****", "*****"); $donereplace = str_ireplace($replaceit, $replaceit2, $p_message); $safe_p_message = mysql_real_escape_string(strip_tags($donereplace)); $safe_p_message2 = str_ireplace("\n", "<br />", $safe_p_message); mysql_query("INSERT INTO forum_posts (post_name, post_msg, time_posted, top_id) VALUES('" . $Yourname . "', '" . $safe_p_message2 . "', '" . $currentime . "', '" . $topic_id . "')"); ?> The variables that you don't see defined here, are defined in my core file. -- DJ! Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/ Share on other sites More sharing options...
marcus Posted August 18, 2007 Share Posted August 18, 2007 Use nl2br. $string = "welcome\nto\nmy\nwebsite"; echo nl2br($string); Outputs: welcome<br>to<br>my<br>website welcome to my website Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/#findComment-327690 Share on other sites More sharing options...
DJTim666 Posted August 18, 2007 Author Share Posted August 18, 2007 that's not working =O! Still the same problem. It doesn't even insert < br / > into the DB. Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/#findComment-327704 Share on other sites More sharing options...
marcus Posted August 18, 2007 Share Posted August 18, 2007 What is it putting into the database, and what does your string hold? Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/#findComment-327706 Share on other sites More sharing options...
DJTim666 Posted August 18, 2007 Author Share Posted August 18, 2007 This code is for a forum that I coded. RIght now the database holds; hello hello testing! I want it to be holding; hello < br / > < br / > hello < br / > < br / > testing! My string holds the content for the textarea. It's for replies to a topic. Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/#findComment-327715 Share on other sites More sharing options...
marcus Posted August 18, 2007 Share Posted August 18, 2007 Try using nl2br to display the data. Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/#findComment-327717 Share on other sites More sharing options...
Masna Posted August 18, 2007 Share Posted August 18, 2007 You're doing something else wrong then. nl2br() should accomplish exactly what you need. It strictly replaces newlines with an HTML break. Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/#findComment-327719 Share on other sites More sharing options...
DJTim666 Posted August 18, 2007 Author Share Posted August 18, 2007 I got it to work when I display the data from the database. But when I use str_replace() to insert data into the DB it should be putting a < br / > on every linbreak. Any idea why that's not working? Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/#findComment-327720 Share on other sites More sharing options...
marcus Posted August 18, 2007 Share Posted August 18, 2007 When you use the str_replace, instead of the \n do you just get n? Because since you're using mysql_real_escape_string it should remove all slashes to prevent an injection. Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/#findComment-327726 Share on other sites More sharing options...
Masna Posted August 18, 2007 Share Posted August 18, 2007 I got it to work when I display the data from the database. But when I use str_replace() to insert data into the DB it should be putting a < br / > on every linbreak. Any idea why that's not working? I suggest you use nl2br upon outputting what's in the database. You should always store a copy of the original content. Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/#findComment-327728 Share on other sites More sharing options...
DJTim666 Posted August 18, 2007 Author Share Posted August 18, 2007 Yes, I see nl2br(); is a much easier way to ouput linebreaks. Thank you very much for that information! -- DJ Quote Link to comment https://forums.phpfreaks.com/topic/65621-solved-str_replace/#findComment-327733 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.