3

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

Alladin
  • 1,010
  • 3
  • 27
  • 45
  • 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. – Brian Logan Mar 01 '16 at 02:35
  • @BrianLogan I have edited the question again, please check and assist me if possible. Thank you – Alladin Mar 01 '16 at 02:44

2 Answers2

2

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

Community
  • 1
  • 1
Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
  • 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\"** – Alladin Mar 01 '16 at 03:36
2

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.

Brian Logan
  • 812
  • 2
  • 6
  • 20
  • 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. – Alladin Mar 01 '16 at 03:40
  • 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. – Brian Logan Mar 01 '16 at 09:17