0

So I have some JSON string that I write to a file, and parse it afterwards, for an export/import tool the problem is on import when I try to parse the file using json_decode it dies because of the caracter escaping.

Here I have an example so you can understand better:

enter image description here

When I write the content I do some basic file_put and json_encode

eg:

$current .= "\n\n\n".'$pagzDynamicPages = \''.json_encode($pagzDynamicPagesData).'\';';

So is there a way to escape this type of strings?

1
  • Maybe you could use; mysql_real_escape_string or the mysqli version of it? Commented Dec 15, 2014 at 13:49

3 Answers 3

2

Try

$current .= "\n\n\n".
    '$pagzDynamicPages = '.
    var_export(json_encode($pagzDynamicPagesData), TRUE).
    ';';

The function var_export() produces a valid PHP expression for the provided data structure.

Sign up to request clarification or add additional context in comments.

3 Comments

I want to offer the file to be available for download...so var_export won't work in this case... I tried... and it displays the content instead of offering it for download
Check its documentation. If you pass TRUE as the second parameter it won't output anything but return the PHP code it produces.
I misplaced a closing parenthesis on the original version of the code. Fixed now and formatted to code to be more readable.
0

You need to escape it properly. Best way probably is heredoc: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Comments

0

You can fix the JSON with str_replace:

$pagzDynamicPagesData = str_replace("'", "\'", $pagzDynamicPages);

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.