S
S
survivor20052020-04-27 16:47:47
PHP
survivor2005, 2020-04-27 16:47:47

json_encode() How to pass back quotes?

Good day guys. Please tell me, I have an array in php, the array has a "txt" key with a large text value, which should go through json to js. But as you know, if the text is large and it has a new line, then js needs back quotes. And php doesn't want to display text in back quotes. How do I pass correctly from php to js array?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-04-27
@rpsv

But as you know, if the text is large and it has a new line, then js needs back quotes. And php doesn't want to display text in back quotes.

Do we know this for sure?)
-
The usual json_encode encodes everything correctly. Try adding the JSON_UNESCAPED_LINE_TERMINATORS flag if the standard result doesn't suit you.
-
When sending to JS, you do not need to do parsing, that is, instead of:
const variable = JSON.parse('<?= json_encode(['...']) ?>');

Need to write:
const variable = <?= json_encode(['...']) ?>;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question