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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.