4

I have problem with parsing HTML strings using json_encode. For example my string:

<h5>Name</h5>

In return my json is ruined because of ">". I found solution in PHP:

json_encode('ARRAY', 'JSON_HEX_QUOT | JSON_HEX_TAG')

But I need to do it in TWIG and it doesn't work there like that. I've got error:

An exception has been thrown during the rendering of a template ("Warning: json_encode() expects parameter 2 to be long, string given") in src/Cloud/ApplicationBundle/Resources/views/Filters/tab.html.twig at line 1.

2 Answers 2

4

This solution work fine for me:

{{ '<h5>Name</h5>'|json_encode(constant('JSON_UNESCAPED_SLASHES')) }}
Sign up to request clarification or add additional context in comments.

Comments

1

Try with

 {{ '<h5>Name</h5>'|json_encode(constant('JSON_HEX_QUOT | JSON_HEX_TAG')) }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.