0

How to store and retrieve a string in this format using php, and I have tried the following the code,

   $description="Welcome to our "world". Some text here! Some text'here!.";

   $description = mysql_real_escape_string($description);

   $description = preg_replace("/&[a-z]+;/i"," ",$description); 

I am using the same variable $description for store and retrieve values in database.In my code not print the single quote.

Need to retrieve the Output :
Welcome to our "world". Some text here! Some text'here!.

Thanks.

Fly_pig
  • 155
  • 8
ahalya
  • 47
  • 1
  • 5
  • Be careful mysql_real_escape_string is now deprecated. Try to use mysqli_real_escape_string ([mysqli_real_escape_string](http://www.php.net/manual/fr/mysqli.real-escape-string.php)) For your simple quote problem, try to add a \ just before it. – user2462805 Jun 12 '13 at 12:30

3 Answers3

1

Your code, as it stands, will error. If you include a " character in a string literal delimited with " characters then you have to escape them so they count as data and not the end of the string.

$description="Welcome to our \"world\". Some text here! Some text'here!.";

I am using the same variable $description for store and retrieve values in database

If you are having problems inserting data into a database, then you should include the code you are using to try to do that.

You should not be using mysql_real_escape_string (which is for preparing data to be inserted into a MySQL database when you are using an an obsolete database API). Use a modern API and parameterized queries.)

You do not need to remove HTML entities from the string. (Your example doesn't have any anyway). If you are dealing with user input data, then you should use a non-destructive way to deal with them (which usually means, if you want to treat the input as text, escaping them with htmlspecialchars immediately before inserting them into an HTML document (not before inserting them into a database).

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0
$description="Welcome to our "world". Some text here! Some text'here!.";


should be

$description="Welcome to our \"world\". Some text here! Some text'here!.";
Goutam Pal
  • 1,763
  • 1
  • 10
  • 14
0

Is it possible that you use ADOdb? This would abstract the db implementation details and I think your needed feature is built-in there.

Manuel Manhart
  • 4,819
  • 3
  • 24
  • 28