8

I have an array of product details, in the product_description string I want to escape some special characters such as: " , \ ..etc but I don't know what should I write exactly to do so, what I did was:

json_encode($array[ProductData][Product_description]);

but then when I checked the result, it gives me errors regarding those special characters.

Here's the product description string:

The 30" Apple Cinema HD Display deliver..etc

The error is in the double-quote.

Can you please assist me on how to do it. Thank you

2
  • 1
    Can you provide an example of what the product description is, and maybe the exact error it is giving? That will help in finding out what is wrong. Commented Mar 1, 2016 at 2:35
  • @BrianLogan I have edited the question again, please check and assist me if possible. Thank you Commented Mar 1, 2016 at 2:44

2 Answers 2

4

You can try with

    $value = json_encode($array[ProductData][Product_description]);
    $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
    $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
    $result = str_replace($escapers, $replacements, $value);

    echo '<pre>';
    print_r($result);
    echo '<pre>';

This is the reference

PHP's json_encode does not escape all JSON control characters

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

1 Comment

I have tried it but the result will be affect even the double quotes of JSON itself: instead of "The 30\" Apple Cinema HD Display deliver..etc" it gives me this result: \"The 30\" Apple Cinema HD Display deliver..etc\"
3

The error you are facing is because the " is not escaped in the array. It most likely looks like the following:

$array = array(
    "ProductData" => array(
        "ProductName": "Apple Cinema Display",
        "ProductDescription" => "The 30" Apple Cinema HD Display deliver..etc"
    )
);

To fix the issue, in your code you should make it the following:

$array = array(
    "ProductData" => array(
        "ProductName": "Apple Cinema Display",
        "ProductDescription" => "The 30\" Apple Cinema HD Display deliver..etc"
    )
);

If you are pulling from a database, this should be done automatically. My suggestion, if you are not using a database, is to manually add the backslashes in where needed. There won't be a reliable function to automatically add them since you are most likely using double quotes around the whole string.

2 Comments

I have tried the str_replace suggested in the other answer, but it's not really working for me; you are right, I am not pulling the data from the database. so by manually adding the backslashes where needed, you mean it like literally add it. because I have so many products that most of them have special characters. this won't be an efficient way to do it.
The best and easiest way to do it would be to make a quick database using MySQL (it is free and open source) and inserting the data into it. It will also allow you to clean up your code a substantial amount.

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.