How do I escape only single quotes?
Quite simply: echo str_replace('\'', '\\\'', $myString);
However, I'd suggest use of JSON and json_encode()
function as it will be more reliable (quotes new lines for instance):
<?php $data = array('myString' => '...'); ?>
<script>
var phpData = <?php echo json_encode($data) ?>;
alert(phpData.myString);
</script>
How to escape single quotes within single quoted strings
If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:
alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234
Explanation of how '"'"'
is interpreted as just '
:
'
End first quotation which uses single quotes."
Start second quotation, using double-quotes.'
Quoted character."
End second quotation, using double-quotes.'
Start third quotation, using single quotes.
If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.
How to escape the single quotes and double quotes at the same time
Use a backslash character (\
) to escape it:
$("#divId").html('<div onclick="location.href=\"xxx\""');
Here are characters that need to be escaped in javascript strings:
- Horizontal Tab:
\t
- Vertical Tab:
\v
- Nul char:
\0
- Backspace:
\b
- Form feed:
\f
- Newline:
\n
- Carriage return:
\r
- Single quote:
\'
- Double quote:
\"
- Backslash:
\\
As stated in the comments, I would do something different in this case:
$('#divId').html('<div id="foo">');
$('#foo').click(function() {
window.location.href = 'xxx';
});
Escape only single quotes (leave double quotes alone) with htmlspecialchars()
str_replace("'", "\\'", $string);
There.
Or, use ENT_QUOTES
htmlspecialchars($string, ENT_QUOTES);
How do I escape a single quote in SQL Server?
Single quotes are escaped by doubling them up, just as you've shown us in your example. The following SQL illustrates this functionality. I tested it on SQL Server 2008:
DECLARE @my_table TABLE (
[value] VARCHAR(200)
)
INSERT INTO @my_table VALUES ('hi, my name''s tim.')
SELECT * FROM @my_table
Results
value
==================
hi, my name's tim.
How do I escape a single quote ( ' ) in JavaScript?
You should always consider what the browser will see by the end. In this case, it will see this:
<img src='something' onmouseover='change(' ex1')' />
In other words, the "onmouseover" attribute is just change(
, and there's another "attribute" called ex1')'
with no value.
The truth is, HTML does not use \
for an escape character. But it does recognise "
and '
as escaped quote and apostrophe, respectively.
Armed with this knowledge, use this:
document.getElementById("something").innerHTML = "<img src='something' onmouseover='change("ex1")' />";
... That being said, you could just use JavaScript quotes:
document.getElementById("something").innerHTML = "<img src='something' onmouseover='change(\"ex1\")' />";
Escaping single quotes in JavaScript string for JavaScript evaluation
The thing is that .replace()
does not modify the string itself, so you should write something like:
strInputString = strInputString.replace(...
It also seems like you're not doing character escaping correctly. The following worked for me:
strInputString = strInputString.replace(/'/g, "\\'");
Related Topics
How to Use the PHP That Brew Installed
The Openssl Extension Is Required For Ssl/Tls Protection
Json_Decode Returns Json_Error_Syntax But Online Formatter Says the Json Is Ok
Find Total Number of Results in MySQL Query With Offset+Limit
Send Post Request Using Volley and Receive in PHP
Get the Files Inside a Directory
Generating a Drop Down List of Timezones With PHP
Verify Receipt For in App Purchase
Retrieving Multiple Result Sets With Stored Procedure in PHP/MySQLi
Fatal Error: Call to Undefined Function Mb_Detect_Encoding()
How to Find the Dates Between Two Specified Date
How to Use PHP Namespaces With Autoload
Converting HTML to Pdf Using PHP
What Is the Syntax For Accessing PHP Object Properties
How to Set Upload_Max_Filesize in .Htaccess