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... Link to comment https://forums.phpfreaks.com/topic/168793-solved-apostrophes-and/ 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. Link to comment https://forums.phpfreaks.com/topic/168793-solved-apostrophes-and/#findComment-890523 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 Link to comment https://forums.phpfreaks.com/topic/168793-solved-apostrophes-and/#findComment-890524 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... Link to comment https://forums.phpfreaks.com/topic/168793-solved-apostrophes-and/#findComment-890532 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']); Link to comment https://forums.phpfreaks.com/topic/168793-solved-apostrophes-and/#findComment-890538 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... Link to comment https://forums.phpfreaks.com/topic/168793-solved-apostrophes-and/#findComment-890547 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.