1

Possible Duplicate:
PHP: Regex to ignore escaped quotes within quotes
How to fix badly formatted JSON in PHP?

I have data that looks literally like this:

"manager1": "Richard "Dick" Smith, MD, MBA",

But it needs to look literally like this for it to work with JSON:

"manager1": "Richard \"Dick\" Smith, MD, MBA",

Note: The difference are backslashes only for the internal double-quotes of the nickname of Dick, while leaving the rest of the string without changes.

I'm having trouble dealing with the credentials (MD, MBA) separated by commas. How can this be done in PHP with regular expressions while only backslashing the internal double-quotes while preserving the rest of the string? Thanks!

This is not a duplicate of How to fix badly formatted JSON in PHP? because that routine can't handle the additional commas from the credentials.

Community
  • 1
  • 1
Edward
  • 9,430
  • 19
  • 48
  • 71
  • 1
    I take it that you don't have a way to fix the problem that has resulted in your data looking like this, so you now need to fix the symptoms? – Tim Pietzcker Nov 06 '12 at 13:56
  • Yes, Tim, I have no control over the source of this data to fix the problem. I unfortunately have to come up with a solution to correct this would-be JSON data. – Edward Nov 06 '12 at 14:01
  • @Quentin, that code can't handle the additional commas this example has to deal with. – Edward Nov 06 '12 at 14:03

2 Answers2

0

I suggest you do it that way:

//suppose your data is in the file named "data.txt". We read it in a variable

$fh = fopen("data.txt");
$res = '';
while (($line = fgets($fh)) !== false) {
   $txt = explode(': ', $line);
   $txt[1] = trim($txt[1], '",'); 
   //now $txt[1] holds the the data with internal quotes but without external quotes
   $txt[1] = preg_replace('@"@', '\"',  $txt[1]);
   //put leading and ending quotes back and join everything back together
   $txt[1] = '"'.$txt[1].'",';
   $res .= implode(': ', $txt);

}

fclose($fh);

//now echo the result with the all internal quotes escaped
echo $res;

I think something like above should work for you.

akhilless
  • 3,169
  • 19
  • 24
  • Thanks for the code. $txt has no value, which make PHP generate for th explode line "PHP Warning: explode() expects parameter 2 to be string, array given in...". – Edward Nov 06 '12 at 15:29
  • right, it should be $txt = explode(': ', $line). I have corrected the code – akhilless Nov 06 '12 at 15:36
  • Also edited the code to handle the ending comma – akhilless Nov 06 '12 at 15:44
  • Thanks for the update. I also added "r" to the fopen to run it. It does a backslash on the last double-quote of each line. – Edward Nov 06 '12 at 15:49
0

The following regular expression pattern:

 /^\s+"[a-z0-9_"]+": "([^"]*".*)",?$/mi

did it for me. See How to correct an invalid JSON in php?

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836