Jump to content

How do I escape quote marks stored in a variable ????


poleposters

Recommended Posts

Hi,

 

I have records in my database with quotes or apostrophes. I am retireving one such record and placing it into a variable.HOwever when I echo the variable it gets all muddled because of the apostrophes.

 

I'm sure there is a function to escape the quotes in a variable but am having trouble finding it.

 

Can someone point the way?

If you are echoing content to the browser, special characters that the browser interprets/renders (quotes, <, >, &) need to be converted using htmlentities() - http://www.php.net/manual/en/function.htmlentities.php

 

Escaping data with a slash \ only has meaning when putting data into a database.

No, neither are working

 

I'll show you the code.

 

 

$bn=Jack's Chicken Shop;

print "<tr><td >Business name</span></td><td><input type='text' name='business_name' value='$bn'></td></tr>";

 

The problem is because the value='$bn' is single quoted and the print "" is double quoted, and I want to print a variable that contains quotes.

Ahh, that's it. Common problem.

 

Problem is HTML doesnt recognize using a backslash to escape... nor does it have an entity for a single quote. Always use double quotes for HTML attributes.

 

$bn="Jack's Chicken Shop is \"the best\"";

print '<tr><td >Business name</span></td><td><input type="text" name="business_name" value="'. htmlentities($bn) .'"></td></tr>';

That did it. I converted all the single quotes to doubles in the HTML and escaped them. The variable printed normally.

 

Except now I have to change all my forms from snigle quotes to double and escape them. I'm in for a long day.

 

Thanks all!

Except now I have to change all my forms from snigle quotes to double and escape them. I'm in for a long day.

 

If that were true I have about two years' work to do "fixing" several thousand scripts.  Luckily, it's not true as a general statement.

Use the ENT_QUOTES parameter in the htmlentities() function. There is no need to change any of your single/double quoting (except as needed to incorporate the htmlentities() function call.) If you use the htmlentities() where the data is retrieved from the database, you won't need to change anything where it is output in the form code.

 

Right on discomatt, I just have one thing to add - don't use double-quotes for variable names unless you have something that needs to be processed within the quotes. For example:

 

discomatt

 

<?php
//slower
$bn="Jack's Chicken Shop is \"the best\"";

//faster
$bn='Jack\'s Chicken Shop is "the best"';

//With the newline char "\n"

//Works
$bn="Jack's Chicken Shop is \"the best\" \n";

//Won't work
$bn='Jack\'s Chicken Shop is "the best" \n';
?>

 

And this only applies to PHP - never use single quotes in (X)HTML.

 

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.