G
G
Grigory Vasilkov2016-01-14 05:33:17
PHP
Grigory Vasilkov, 2016-01-14 05:33:17

How to pass layout via JSON to Javascipt from PHP?

I ate all my brains already =)
In general, the authors who wrote the site obviously did not know that it is very unsafe to use simultaneously in a language file:
1. single quotes
2. double quotes
3. line
breaks 4. layout and tags
5. percent signs in symbols %s, %d and so on for sprintf
Now the json_encode($data, JSON_HEX_QUOT) function does not cope with the direct task.
I take it, I turn it into JSON, then I output it in the template as a
php function:

$data = get_langs();
$json = json_encode($data, JSON_HEX_QUOT);
$smarty->assign('json', $json);

template.htm
<script>
App.Langs.addJson('{$json}');
</script>

If you put single quotes - swears with single quotes.
If double - with double.
I also tried like this:
str_replace("\"", '\"', str_replace("'", '\'', json_encode($data)))

Well, not at all! The last time it showed a conflict at the% sign, I thought the movement - and shish, it replaced the double quote with two slashes instead of one for some reason.
Is there anyone to help?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2016-01-14
@gzhegow

php.net/manual/en/function.base64-encode.php
And in JS:
PS:

$json = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE); ///вот так - не работает?!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question