-1

here is my json string { "opa": "O_\ufe0fP\ufe0f_\ufe0fA\ufe0f" }

when i decode my json string object(stdClass)#16 (1) { ["opa"]=> string(25) "O_ufe0fPufe0f_ufe0fAufe0f" }

i want to get without this ->\ufe0f symbols result should be "O_P_A"

P.S. it can be any other character like [\udbff, \udc00, etc.]

1 Answers1

0

Your string in the JSON contains 4 special Unicode characters Variation Selector-16 (VS16). With a special debug class, this string can be shown as a PHP-compliant string notation.

$jsonStr = '{ "opa": "O_\ufe0fP\ufe0f_\ufe0fA\ufe0f" }';
$obj = json_decode($jsonStr);
$stringWithVS16 = $obj->opa;

debug::writeUni($stringWithVS16); 
//string(17) UTF-8mb3 "O_\u{fe0f}P\u{fe0f}_\u{fe0f}A\u{fe0f}"

These special Unicode characters are real and this fact has nothing to do with the encoding or decoding of the JSON. If these signs bother you, all you have to do is remove them. In the simplest case, str_replace can do this.

$cleanString = str_replace("\u{fe0f}","",$stringWithVS16);
debug::writeUni($cleanString);
//string(5) ASCII "O_P_A" 

That's just a fix for that particular character. If other special characters bother you, you need to find a regular expression that removes all such characters with preg_replace.

jspit
  • 7,276
  • 1
  • 9
  • 17