-1

I have this string that was working with json_decode() with PHP 5.6 but doesn't work with PHP 7.2?

$json = '
{
  "text": "Hi {{fname}} 
   Welcome to our customer support. 
   Please select language to proceed",
  "buttons": [
    {
      "text": "English",
      "value": "language_english",
      "type": "postback"
    },
    {
      "text": "Germany",
      "value": "language_germany",
      "type": "postback"
    }
  ]
}';

I have tried replacing the whitespaces and newline like this

$json = preg_replace("/\n/m", '\n', $json);
$what   = "\\x00-\\x19"; // all whitespace characters except space itself
$json = trim(preg_replace( "/[".$what."]+/" , '' , $json));

Which results into a string like this

\n{\n  "text": "Hi {{fname}} \n   Welcome to our customer support. \n   Please select language to proceed",\n  "buttons": [\n    {\n      "text": "English",\n      "value": "language_english",\n      "type": "postback"\n    },\n    {\n      "text": "Germany",\n      "value": "language_germany",\n      "type": "postback"\n    }\n  ]\n}

Notice the \n in between and outside double quotes which makes it an invalid json and hence json_decode won't work in this case.

Does anyone know a way to achieve this?

Thank you.

Mutinda Boniface
  • 518
  • 1
  • 5
  • 14

1 Answers1

0
{
    "text": "Hi {{fname}} \n Welcome to our customer support. \n Please select language to proceed",
    "buttons": [{
            "text": "English",
            "value": "language_english",
            "type": "postback"
        },
        {
            "text": "Germany",
            "value": "language_germany",
            "type": "postback"
        }
    ]
}

This is a valid json. i added line breaks so you can use them if you want to print the messages in browser.

You can use this nice tool to validate your json when you have doubts.

Editing my answer based on comment feedback.

First of all the right step is that you figure out why you have a broken json in the database. If it's not on you and you have to fix it in php then a solution could be like this:

<?php
echo '<pre>';
$data = '
{
  "text": "Hi {{fname}} 
   Welcome to our customer support. 
   Please select language to proceed",
  "buttons": [
    {
      "text": "English",
      "value": "language_english",
      "type": "postback"
    },
    {
      "text": "Germany",
      "value": "language_germany",
      "type": "postback"
    }
  ]
}';


        if (strstr($data, "\n")) {
            $data = trim(preg_replace('/\s\s+/', '  ', $data));
        }

        echo $data;

Above code will capture the line breaks of your text field, and replace them with DOUBLE space. Then you will get a valid json like:

{
"text": "Hi {{fname}}  Welcome to our customer support.  Please select language to proceed",
"buttons": [
{
"text": "English",
"value": "language_english",
"type": "postback"
},
{
"text": "Germany",
"value": "language_germany",
"type": "postback"
}
]
}

What you can do if you need the line breaks is that you decode your json (just like you want and replace the double spacing in text field with a line break

pr1nc3
  • 8,108
  • 3
  • 23
  • 36