Skip to content

Instantly share code, notes, and snippets.

@lorenzoaiello
Created October 21, 2019 13:02
Show Gist options
  • Save lorenzoaiello/4e78a35512618983c021b674b5f38583 to your computer and use it in GitHub Desktop.
Save lorenzoaiello/4e78a35512618983c021b674b5f38583 to your computer and use it in GitHub Desktop.
PHP JSON String Fixer
function fixJSON( $j ){
$j = trim( $j );
$j = ltrim( $j, '(' );
$j = rtrim( $j, ')' );
$a = preg_split('#(?<!\\\\)\"#', $j );
for( $i=0; $i < count( $a ); $i+=2 ){
$s = $a[$i];
$s = preg_replace('#([^\s\[\]\{\}\:\,]+):#', '"\1":', $s );
$a[$i] = $s;
}
$j = implode( '"', $a );
return $j;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment