0

I create json string with out array by using user input But some time error can be occur user input some JSON character such as , " { [ ..etc

I should want to escape all character and i sholud want to show user input data without any change

ex. "I'm " i should want show this input without change

1 Answers1

-1

you need to escape your characters:

$pattern = '/([{|}|\[|\]|\'|\"])/gi';
$replacement = '\${1}';
echo preg_replace($pattern, $replacement, $string);
Froggiz
  • 683
  • 8
  • 13
  • This didn't work for me. The 'g' at the end of the pattern causes preg_replace to return nothing, and it's not actually needed because preg_replace already replaces all occurrences of the pattern. Also, the replacement should be '\\\\${1}', which inserts a single backslash in front of the character found in the pattern. A better answer is here: http://stackoverflow.com/a/3615890/204842 – Russell G Feb 11 '18 at 19:31