# Is there a PHP function that only adds slashes to double quotes NOT single quotes

PHPServer Side ProgrammingProgramming

The json_encode function can be used to add slashes to double quotes. Also ‘addcslahses’ can also be used to add ‘\’ to specific characters −

## Example

Live Demo

<?php
$str = addcslashes("Hello there!","t"); echo($str);
?>

## Output

This will produce the following output −

Hello \there!

The ‘addcslashes’ function is used to return a string with backslashes in front of the specific characters. It is a case sensitive function and should usually not be used with 0 (null), r (carriage return), n (newline), f (form read), t (tab), v (vertical tab) values. This is because values like \0, \r, \n, \t, \f and \v are pre-define escape sequences.

In the above code, the ‘addcslashes’ function is called on the string by specifying that the backslash has to occur before the character ‘t’.

Published on 09-Apr-2020 11:13:03
Advertisements