There are a couple of functions that could be used:
<?php
$string = 'string test"';
echo htmlentities($string) . "\n";
echo addslashes($string) . "\n";
They produce the following:
string test"
string test\"
Answer from Nick on Stack OverflowEscaping quotation marks in PHP - Stack Overflow
php - Escape double quotes with variable inside HTML echo - Stack Overflow
Escape quotes in a variable with PHP
escape all quotes and accents in php variable for javascript function - Stack Overflow
There are a couple of functions that could be used:
<?php
$string = 'string test"';
echo htmlentities($string) . "\n";
echo addslashes($string) . "\n";
They produce the following:
string test"
string test\"
If you are relying on user input, use htmlentities($param, ENT_QUOTES);
See http://uk.php.net/manual/en/function.htmlentities.php
Use a backslash as such
"From time to \"time\"";
Backslashes are used in PHP to escape special characters within quotes. As PHP does not distinguish between strings and characters, you could also use this
'From time to "time"';
The difference between single and double quotes is that double quotes allows for string interpolation, meaning that you can reference variables inline in the string and their values will be evaluated in the string like such
$name = 'Chris';
$greeting = "Hello my name is $name"; //equals "Hello my name is Chris"
As per your last edit of your question I think the easiest thing you may be able to do that this point is to use a 'heredoc.' They aren't commonly used and honestly I wouldn't normally recommend it but if you want a fast way to get this wall of text in to a single string. The syntax can be found here: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc and here is an example:
$someVar = "hello";
$someOtherVar = "goodbye";
$heredoc = <<<term
This is a long line of text that include variables such as $someVar
and additionally some other variable $someOtherVar. It also supports having
'single quotes' and "double quotes" without terminating the string itself.
heredocs have additional functionality that most likely falls outside
the scope of what you aim to accomplish.
term;
Use the addslashes function:
$str = "Is your name O'Reilly?";
// Outputs: Is your name O\'Reilly?
echo addslashes($str);
Some tips on outputting HTML with PHP:
- Use single quotes so that you don't have to escape the double quotes (when using echo),
- Use
htmlspecialchars()to properly escape any "rogue" values you may have.
Example using echo:
echo '<input type="hidden" name="id" value="', htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'), '" />';
Or printf():
printf('<input type="hidden" name="id" value="%s" />',
htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8')
);
Or, in HTML mode:
?>
<input type="hidden" name="id" value="<?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?>" />
<?php
Use htmlentities:
echo "<input type=\"hidden\" name=\"id\" value=\"".htmlentities($row['id'])."\" />";
There are a couple of functions that could be used:
<?php
$string = 'string test"';
echo htmlentities($string) . "\n";
echo addslashes($string) . "\n";
They produce the following:
string test"
string test\"
If you are relying on user input, use htmlentities($param, ENT_QUOTES);
See http://uk.php.net/manual/en/function.htmlentities.php
To display the string "as it is" in a browser, you must pass it through htmlspecialchars().
echo htmlspecialchars($variable);
If you don't, the browser interprets the HTML and displays the link text, as expected.
I tried to use your exact code in a php page I have and it acts as you need it to. Seems to be working just fine for me.