Jump to content

Replace Quotes in a Variable With Slash-Quotes


legohalflife2man

Recommended Posts

Let's say I have an entry in a database with some double quotes and single quotes in it. So, I call the content from the database using a code like this:

 

<?php
$sql = "SELECT news_content FROM news";
$result = mysql_query($sql) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$news_content = $row['news_content'];
?>

 

So, if I echo the $new_content variable, it will show the content that was selected from the database, right? That is correct. Now, you might be wondering what my problem is. Well, I want to replace all of the double quotes and single quotes in the $news_content variable with \" and \' (with backslashes before the quotes) instead of just having the quotes appear normally as " and '. How can I do this? Should I use str_replace() or something such as that? Please provide some insight on this subject for me. Thank you in advance, and feel free to ask me to explain my question in more detail, if needed.

just use htmlentities($var, ENT_QUOTES);

 

Yes, but I forgot to mention that there are some HTML codes in the $news_content variable that need to keep their quotes so that they can be parsed. Sorry about missing that fact. So, are there any other ideas?

 

So, why do you want to escape quotes in content that you just got from a database?

 

I want to place a PHP variable in as a JavaScript variable, so the quotes need to have slashes before them in order for the JavaScript variable to act appropriately.

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.