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&quot;
string test\"
Answer from Nick on Stack Overflow
🌐
Delft Stack
delftstack.com › home › howto › php › php escape quotes
How to Escape Quotation Marks in PHP | Delft Stack
February 2, 2024 - Thus, it will display a complete direct speech sentence with double quotes. ... She asked me, " Are you going out tonight ?". We can use the heredoc syntax <<< to escape quotation marks from a string in PHP.
🌐
PHP
php.net › manual › en › function.addslashes.php
PHP: addslashes - Manual
Addslashes is *never* the right answer, it's (ab)use can lead to security exploits! if you need to escape HTML, it's (unfortunately) <?php echo htmlentities($html, ENT_QUOTES|ENT_SUBSTITUTE|ENT_DISALLOWED); ?> if you need to quote shell arguments, it's <?php $cmd.= " --file=" . escapeshellarg($arg); ?> if you need to quote SQL strings it's <?php $sql.= "WHERE col = '".$mysqli->real_escape_string($str)."'"; ?> or <?php $sql.= "WHERE col = " . $pdo->quote($str); ?> if you need to quote javascript/json strings its <?php let str = <?=json_encode($str, JSON_THROW_ON_ERROR);?>; ?> if you need to quo
Discussions

Escaping quotation marks in PHP - Stack Overflow
I am getting a parse error, and I think it's because of the quotation marks over "time". How can I make it treat it as a whole string? More on stackoverflow.com
🌐 stackoverflow.com
php - Escape double quotes with variable inside HTML echo - Stack Overflow
For a variable inside a echo that contains HTML, where would I add slashes to escape the double quotes? Example: echo " "; This part: More on stackoverflow.com
🌐 stackoverflow.com
Escape quotes in a variable with PHP
I use this code to genefate html echo " "; Everything would be OK unless $param contains ' or " character. How should it be implemented to More on stackoverflow.com
🌐 stackoverflow.com
April 16, 2011
escape all quotes and accents in php variable for javascript function - Stack Overflow
In case you want to do this multiple times, for example in a loop in a list of strings, you can also work around it (unless you want to corrupt the original php strings), by: More on stackoverflow.com
🌐 stackoverflow.com
May 7, 2019
Top answer
1 of 7
98

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;
2 of 7
76

Use the addslashes function:

 $str = "Is your name O'Reilly?";

 // Outputs: Is your name O\'Reilly?
   echo addslashes($str);
🌐
BUSoft Technologies
busofttech.com › home › php escape quotes how to do it right?
PHP Escape Quotes: How to Do It Right | BUSoftTech
March 10, 2026 - The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the escape sequences. Each variable will be replaced by its value. ... <?php // A Simple String echo “I am a developer.”; echo “\n”; Echo “it will be interesting to know about the string.”; Echo “\n”; //variables are included directly $string = ‘abc’; echo “the word is $string”; ?>
Find elsewhere
🌐
Itsourcecode
itsourcecode.com › home › php escape quotes with detailed explanation
PHP Escape Quotes With Detailed Explanation - Itsourcecode.com
November 17, 2022 - The special case allows you to display a literal (single-quote), then it will escape with a backslash (\) and in some instances that you want to display a backslash, you can easily escape it with an additional backslash(\\). ... <?php // characters ...
🌐
TinyMCE
tiny.cloud › blog › php-escape
How to work with PHP escape quotes and characters | TinyMCE
December 7, 2023 - It covers a few methods as well as provides demos that look specifically at quotations, and makes use of the TinyMCE rich text editor. PHP escape mechanisms help to prevent syntax errors. Unexpected identifier errors in PHP, for instance, can result from mishandled characters like single or double quotes.
🌐
Lucidar
lucidar.me › en › web-dev-class › lesson-4-06-php-quotes
Lesson 4.6. PHP quotes | Lulu's blog
September 17, 2022 - Note that when tables with indexes are encapsulated in strings, they must be escaped with curly brackets as on the following example: ... <?php $variable = 4; $table = array (10,20,30); // Display $variable (single quotes) echo 'Variable: $variable\n'; // Display 4 (double quotes) echo "Variable: $variable\n"; // Display 20 (table with double quotes and curly brackets) echo "Table: {$table[1]}\n"; ?>
🌐
Stack Overflow
stackoverflow.com › questions › 56023393 › escape-all-quotes-and-accents-in-php-variable-for-javascript-function
escape all quotes and accents in php variable for javascript function - Stack Overflow
May 7, 2019 - <html> <?php $var="\'quotes\" àccènts"; // 'quotes" àccènts ?> <button onclick="fun('<?php echo $var ?>');">click me</button> </html> but when I click I get "Uncaught SyntaxError: Invalid or unexpected token" in the console ... <button onclick="fun('<?php echo $var ?>');">click me</button> <?php function escape($var) { $var=str_replace("'","\\'",$var); $var=str_replace("\"","&quot;",$var); $var=trim(preg_replace("/\s+/"," ",$var)); return $var } ?>
🌐
SitePoint
sitepoint.com › php
Best way to escape single quotes - PHP - SitePoint Forums | Web Development & Design Community
September 1, 2016 - This apparently is the wrong way: $popupContent .= " "; Suggestions?
🌐
SitePoint
sitepoint.com › php
Escaping double and single quotes - PHP - SitePoint Forums | Web Development & Design Community
May 19, 2015 - Hi, Here’s what I have. foreach ($author_query as $author) { echo $author['name'].", "; } I am trying to add a href link to it, but I’m having issues with quotes. What’s the right way to do this? The code below doesn’t work because I already opened single quotes with a href, then tried to use it with person_id. foreach ($author_query as $author) { echo "".$author['name'].", ". ""; }
🌐
Nelko Dev
nelkodev.com › en › blog › mastering-quotes-in-php-strategies-and-security
Mastering Quotes in PHP: Strategies and Security
June 3, 2024 - ... For the secure handling of information presented in HTML, functions such as htmlspecialchars o htmlentities. ... Always use prepared statements for database operations. Enclose variables in quotes only when necessary.
🌐
FlatCoding
flatcoding.com › home › php escape characters: how to escape special characters
PHP Escape Characters: How to Escape Special Characters - FlatCoding
April 25, 2025 - When you want to include a quote mark inside a string that uses the same quote type, use \" or \' to avoid ending the string early. When you want to include a backslash itself, use \\ because a single backslash starts an escape sequence.
🌐
O'Reilly
oreilly.com › library › view › programming-php-3rd › 9781449361068 › ch04s01.html
Quoting String Constants - Programming PHP, 3rd Edition [Book]
February 15, 2013 - The simpler of the two ways is to put the variable name in a double-quoted string or heredoc: $who = 'Kilroy'; $where = 'here'; echo "$who was $where"; Kilroy was here · The other way is to surround the variable being interpolated with curly braces.
Authors: Rasmus LerdorfKevin TatroePeter MacIntyre
Published: 2013
Pages: 538