How can i clean this JSON String data ?. At first look it is very easy using str_replace method but its not. This JSON string is form JSON object that has spaces for ex . {" First Name ": "something"} . so when i converted this into json string the empty spaces replaced by unwanted strings(\ ) . I think this problem can be solve using preg_replace but the suddenly i dont know the regex for comma,double quote,colon. This characters is necessary for json string format. Please help me.
for example
{"AS_applicant_Data__c":
"{
\"Last Name\u00a0\":\"SDFSAD\",
\"First Name\u00a0\":\"SDFAFSDA\",
\"Middle Name\u00a0\":\"SAFDSAFD\",
\"Gender\u00a0\":\"Male\"
}"
}
to
{"AS_applicant_Data__c":"
"{
"Last Name":"SDFSAD",
"First Name":"SDFAFSDA",
"Middle Name":"SAFDSAFD",
"Gender":"Male"
}"
}
I use this code and it looks ok to me:
<?php
$string = <<<EOD
{"AS_applicant_Data__c":
"{
\"Last Name\u00a0\":\"SDFSAD\",
\"First Name\u00a0\":\"SDFAFSDA\",
\"Middle Name\u00a0\":\"SAFDSAFD\",
\"Gender\u00a0\":\"Male\"
}"
}
EOD;
$pattern = '#\\\\u[0-9a-f]{4}#i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>
This regular expression is blindly replace Unicode char in a format of \\uxxxx with empty string. If you know for certain that there's only \ , you can change regex to #\\\\\\\ #i
Try a quick run here: http://ideone.com/xW4zTN
When i use this
preg_replace("/[^a-zA-Z]/", "", $str);
.. it will remove all the special characters. is it possible to skip the comma,double quotes,curly braces and colon ?
Of course it is, just include those special characters in the character class (the quotation mark has to be escaped with a backslash):
preg_replace("/[^a-zA-Z,\"{}:]/", "", $str);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.