shamuraq Posted August 4, 2009 Share Posted August 4, 2009 I have a mix of both <br> and apostrophes in my multiline form entry that i need to send to MySQL BLOB. When i searched online i found that i can use str_replace(). When i tried coding it i realise i can only use it only for either <br> or apostrophe not both. I tried using mysql_real_escape_string() but nothing was sent to database. Below is my current stable script only replacing <br> with "/n". How do i add apostrophe string replace? //$challenge=$_POST['challenge']; $challenge = str_replace("\n","<br>",$_POST['challenge']); //$solutions=$_POST['solutions']; $solutions = str_replace("\n","<br>",$_POST['solutions']); //$remarks=$_POST['remarks']; $remarks = str_replace("\n","<br>",$_POST['remarks']); Thanx in advance... Quote Link to comment Share on other sites More sharing options...
jonsjava Posted August 4, 2009 Share Posted August 4, 2009 $challenge = str_replace("'","\'",nl2br($_POST['challenge'])); That should fix you up. Quote Link to comment Share on other sites More sharing options...
Bjom Posted August 4, 2009 Share Posted August 4, 2009 There's a lot of possible ways of changing your original string depending on what you want to actually achieve. If you simply need to replace two things in one go, have a look at strtr or choose a different function from this list Quote Link to comment Share on other sites More sharing options...
shamuraq Posted August 4, 2009 Author Share Posted August 4, 2009 $challenge = str_replace("'","\'",nl2br($_POST['challenge'])); That should fix you up. Jons, can you explain a bit to me the breakdown of your code? Because when i call it back from MySQL i need to replace it back to its original form... Quote Link to comment Share on other sites More sharing options...
jonsjava Posted August 4, 2009 Share Posted August 4, 2009 sure. str_replace("'", "\'", $_POST['challenge']) I'm sure you know what that does: it changes ' to \' Next: nl2br() is the short code for changing new lines to <br /> I just made the code smaller by doing it all in one line. so, when you pull it out of the database, you just do this: <?php //db connection //db query //results //lets assume you put your results into array $row, and the column that has challenge is called "challenge" $challenge = str_replace("\'","'", $row['challenge']); // if you want to remove the <br />, do it this way:str_replace("\'","'", str_replace("<br />, "\n", $row['challenge']); Quote Link to comment Share on other sites More sharing options...
shamuraq Posted August 4, 2009 Author Share Posted August 4, 2009 Thanx mate. I actually wanted to know the 'nl2br'. Thanx for the xplanation m8... Quote Link to comment 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.