I need a backslash to be a part of a string. How can I do it?
PHP: addslashes() function The addslashes() function is used to add backslashes in front of the characters that need to be quoted. The predefined characters are single quote ('), double quote("), backslash(\) and NULL (the NULL byte).
If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: var s = "\\Tasks"; // or var s = @"\Tasks"; Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.
The stripslashes() function removes backslashes added by the addslashes() function. Tip: This function can be used to clean up data retrieved from a database or from an HTML form.
$access_token = "1\/MgotwOvbwZN9MVxH5PrLR2cpvX1EJl8omgYdA9rrjx8"; $access_token = preg_replace('/\\\//', '/', $access_token);
When the backslash \
does not escape the terminating quote of the string or otherwise create a valid escape sequence (in double quoted strings), then either of these work to produce one backslash:
$string = 'abc\def'; $string = "abc\def"; // $string = 'abc\\def'; $string = "abc\\def";
When escaping the next character would cause a parse error (terminating quote of the string) or a valid escape sequence (in double quoted strings) then the backslash needs to be escaped:
$string = 'abcdef\\'; $string = "abcdef\\"; $string = 'abc\012'; $string = "abc\\012";
Short answer:
Use two backslashes.
Long answer:
You can sometimes use a single backslash, but sometimes you need two. When you can use a single backslash depends on two things:
If you have a double quote string the backslash is treated as an escape character in many cases so it is best to always escape the backslash with another backslash:
$s = "foo\\bar"
In a single quoted string backslashes will be literal unless they are followed by either a single quote or another backslash. So to output a single backslash with a single quoted string you can normally write this:
$s = 'foo\bar'
But to output two backslashes in a row you need this:
$s = 'foo\\\\bar'
If you always use two backslashes you will never be wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With