S
S
Stadinov Denis2015-09-25 13:36:34
PHP
Stadinov Denis, 2015-09-25 13:36:34

How to wean json_encode in php to escape the backslash "\"?

Hello!
The situation is, there is a multidimensional php array, it has regular expressions (in string type), all this is transferred to javascript using json.
But php finds backslashes in the string and escapes them, and as a result we get two backslashes :(
Code, for playing around:

<?php $array = [
    ['regExp' => '^[\+]{1}[7]{1}[\d]{10}$'],
    ['regExp' => '^\+\d{7,15}$'],
    ['regExp' => '([45]{1}[\d]{15}|[6]{1}[\d]{17})$'],
]; ?>

<script> var test = JSON.parse('<?= json_encode($array) ?>'); 
console.log(test); </script>

Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
lyeskin, 2015-09-25
@lyeskin

json_encode($str, JSON_UNESCAPED_SLASHES);
As they say here - "banned in Google?". stackoverflow.com/questions/10210338/json-encode-e...

K
Kirill, 2015-09-25
@kirill89

No need to hang up JSON.parse

var test = <?= json_encode($array) ?>;
console.log(test);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question