Jump to content

[SOLVED] Apostrophes and <br>


shamuraq

Recommended Posts

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

$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...

 

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']);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.