0

After encoding by json_encode in PHP, I want pretty json and want to remove Unicode and latin character from the string or JSON. I want this for api, needed clean response.

Below is the unicoded json after json_encode.

E.g.

\u003Cb\u003EInformation about \u003Ci\u003E\u0027Sachin Tendulkar\u0027\u003C\/i\u003E\u003C\/b\u003E\u003Cbr\u003ESachin Ramesh Tendulkar \u003Ci\u003E (\/\u02ccs\u0259t\u0283\u026an t\u025bn\u02c8du\u02d0lk\u0259r\/; born 24 April 1973) \u003C\/i\u003E is a former Indian cricketer and captain, widely regarded to be one of the greatest cricketers of all time and by many as the greatest batsman of all time. He took up cricket at the age of eleven, made his Test debut on 15 November 1989 against Pakistan in Karachi at the age of sixteen, and went on to represent Mumbai domestically and India internationally for close to twenty-four years. He is the only player to have scored one hundred international centuries, the first batsman to score a double century in a One Day International, holds the record for most number of runs in both ODI and Test cricket, the only player to complete more than 30,000 runs in international cricket.\u003Cbr\u003E\u003Cbr\u003E\u003Cb\u003E\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003EPersonal information\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003EFull name:\u003C\/b\u003E Sachin Ramesh Tendulkar\u003Cbr\u003E\u003Cb\u003EBorn:\u003C\/b\u003E (1973-04-24) 24 April 1973 (age\u00a042) Mumbai, Maharashtra, India\u003Cbr\u003E\u003Cb\u003ENickname:\u003C\/b\u003E Tendlya, Bombay Bomber, Little Master, Master Blaster \u003Cbr\u003E\u003Cb\u003EHeight:\u003C\/b\u003E 5\u00a0ft 5\u00a0in (165\u00a0cm)\u003Cbr\u003E\u003Cb\u003EBatting style:\u003C\/b\u003E Right-handed\u003Cbr\u003E\u003Cb\u003EBowling style:\u003C\/b\u003E Right-arm medium, leg break, off break\u003Cbr\u003E\u003Cb\u003ERole:\u003C\/b\u003E Batsman\u003Cbr\u003E\u003Cb\u003E\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003EInternational information\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003ENational side:\u003C\/b\u003E India\u003Cbr\u003E\u003Cb\u003ETest debut (cap\u00a0187):\u003C\/b\u003E 15 November 1989\u00a0v\u00a0Pakistan\u003Cbr\u003E\u003Cb\u003ELast Test:\u003C\/b\u003E 14 November 2013\u00a0v\u00a0West Indies\u003Cbr\u003E\u003Cb\u003EODI debut (cap\u00a074):\u003C\/b\u003E 18 December 1989\u00a0v\u00a0Pakistan\u003Cbr\u003E\u003Cb\u003ELast ODI:\u003C\/b\u003E 18 March 2012\u00a0v\u00a0Pakistan\u003Cbr\u003E\u003Cb\u003EODI shirt no.:\u003C\/b\u003E 10\u003Cbr\u003E\u003Cb\u003EOnly T20I (cap\u00a011):\u003C\/b\u003E 1 December 2006\u00a0v\u00a0South Africa\u003Cbr\u003E\u003Cb\u003E\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003EDomestic team information\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003E1988:\u003C\/b\u003E Cricket Club of India\u003Cbr\u003E\u003Cb\u003E1988\u20132013:\u003C\/b\u003E Mumbai\u003Cbr\u003E\u003Cb\u003E1992:\u003C\/b\u003E Yorkshire\u003Cbr\u003E\u003Cb\u003E2008\u20132013:\u003C\/b\u003E Mumbai Indians\u003Cbr\u003E\u003Cb\u003E2014:\u003C\/b\u003E Marylebone Cricket Club\u003Cbr\u003E\u003Cb\u003E\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003ECareer statistics\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003ECompetition \/ Test \/ ODI \/ FC \/ LA Matches:\u003C\/b\u003E 200 \/ 463 \/ 310 \/ 551\u003Cbr\u003E\u003Cb\u003ERuns scored:\u003C\/b\u003E 15,921 \/ 18,426 \/ 25,396 \/ 21,999\u003Cbr\u003E\u003Cb\u003EBatting average:\u003C\/b\u003E 53.78 \/ 44.83 \/ 57.84 \/ 45.54\u003Cbr\u003E\u003Cb\u003E100s\/50s:\u003C\/b\u003E 51\/68 \/ 49\/96 \/ 81\/116 \/ 60\/114\u003Cbr\u003E\u003Cb\u003ETop score:\u003C\/b\u003E 248* \/ 200* \/ 248* \/ 200*\u003Cbr\u003E\u003Cb\u003EBalls bowled:\u003C\/b\u003E 4,240 \/ 8,054 \/ 7,605 \/ 10,230\u003Cbr\u003E\u003Cb\u003EWickets:\u003C\/b\u003E 46 \/ 154 \/ 71 \/ 201\u003Cbr\u003E\u003Cb\u003EBowling average:\u003C\/b\u003E 54.17 \/ 44.48 \/ 61.74 \/ 42.17\u003Cbr\u003E\u003Cb\u003E5 wickets in innings:\u003C\/b\u003E 0 \/ 2 \/ 0 \/ 2\u003Cbr\u003E\u003Cb\u003E10 wickets in match:\u003C\/b\u003E 0 \/ n\/a \/ 0 \/ n\/a\u003Cbr\u003E\u003Cb\u003EBest bowling:\u003C\/b\u003E 3\/10 \/ 5\/32 \/ 3\/10 \/ 5\/32\u003Cbr\u003E\u003Cb\u003ECatches\/stumpings:\u003C\/b\u003E 115\/\u2013 \/ 140\/\u2013 \/ 186\/\u2013 \/ 175\/\u2013"

Output should be:

Information about 'Sachin Tendulkar'
Sachin Ramesh Tendulkar (/ˌsətʃɪn tɛnˈduːlkər/; born 24 April 1973) is a former Indian cricketer and captain, widely regarded to be one of the greatest cricketers of all time and by many as the greatest batsman of all time. He took up cricket at the age of eleven, made his Test debut on 15 November 1989 against Pakistan in Karachi at the age of sixteen, and went on to represent Mumbai domestically and India internationally for close to twenty-four years. He is the only player to have scored one hundred international centuries, the first batsman to score a double century in a One Day International, holds the record for most number of runs in both ODI and Test cricket, the only player to complete more than 30,000 runs in international cricket.


Personal information 
Full name: Sachin Ramesh Tendulkar
Born: (1973-04-24) 24 April 1973 (age 42) Mumbai, Maharashtra, India
Nickname: Tendlya, Bombay Bomber, Little Master, Master Blaster 
Height: 5 ft 5 in (165 cm)
Batting style: Right-handed
Bowling style: Right-arm medium, leg break, off break
Role: Batsman

International information 
National side: India
Test debut (cap 187): 15 November 1989 v Pakistan
Last Test: 14 November 2013 v West Indies
ODI debut (cap 74): 18 December 1989 v Pakistan
Last ODI: 18 March 2012 v Pakistan
ODI shirt no.: 10
Only T20I (cap 11): 1 December 2006 v South Africa

Domestic team information 
1988: Cricket Club of India
1988–2013: Mumbai
1992: Yorkshire
2008–2013: Mumbai Indians
2014: Marylebone Cricket Club

Career statistics 
Competition / Test / ODI / FC / LA Matches: 200 / 463 / 310 / 551
Runs scored: 15,921 / 18,426 / 25,396 / 21,999
Batting average: 53.78 / 44.83 / 57.84 / 45.54
100s/50s: 51/68 / 49/96 / 81/116 / 60/114
Top score: 248* / 200* / 248* / 200*
Balls bowled: 4,240 / 8,054 / 7,605 / 10,230
Wickets: 46 / 154 / 71 / 201
Bowling average: 54.17 / 44.48 / 61.74 / 42.17
5 wickets in innings: 0 / 2 / 0 / 2
10 wickets in match: 0 / n/a / 0 / n/a
Best bowling: 3/10 / 5/32 / 3/10 / 5/32
Catches/stumpings: 115/– / 140/– / 186/– / 175/–"
Cœur
  • 37,241
  • 25
  • 195
  • 267
user974
  • 41
  • 7
  • What is the input? Also, what options are you adding the to encoder? – Flosculus Nov 23 '15 at 14:16
  • `json_encode` has an option to do that, see [Any way to return PHP \`json\_encode\` with encode UTF-8 and not Unicode?](http://stackoverflow.com/questions/6771938/any-way-to-return-php-json-encode-with-encode-utf-8-and-not-unicode) – roeland Nov 23 '15 at 20:21
  • JSON is by definition UTF-8, which is Unicode and which in turn is a clean response. Live with it or use a different metaformat. In other words, your question doesn't make much sense and I don't understand where you think that you have a problem. – Ulrich Eckhardt Dec 28 '15 at 11:59

1 Answers1

-1

You have to translate the embedded unicode escape sequences back to their corresponding characters.

$data = "\u003Cb\u003EInformation about \u003Ci\u003E\u0027Sachin Tendulkar\u0027\u003C\/i\u003E\u003C\/b\u003E\u003Cbr\u003ESachin Ramesh Tendulkar \u003Ci\u003E (\/\u02ccs\u0259t\u0283\u026an t\u025bn\u02c8du\u02d0lk\u0259r\/; born 24 April 1973) \u003C\/i\u003E is a former Indian cricketer and captain, widely regarded to be one of the greatest cricketers of all time and by many as the greatest batsman of all time. He took up cricket at the age of eleven, made his Test debut on 15 November 1989 against Pakistan in Karachi at the age of sixteen, and went on to represent Mumbai domestically and India internationally for close to twenty-four years. He is the only player to have scored one hundred international centuries, the first batsman to score a double century in a One Day International, holds the record for most number of runs in both ODI and Test cricket, the only player to complete more than 30,000 runs in international cricket.\u003Cbr\u003E\u003Cbr\u003E\u003Cb\u003E\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003EPersonal information\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003EFull name:\u003C\/b\u003E Sachin Ramesh Tendulkar\u003Cbr\u003E\u003Cb\u003EBorn:\u003C\/b\u003E (1973-04-24) 24 April 1973 (age\u00a042) Mumbai, Maharashtra, India\u003Cbr\u003E\u003Cb\u003ENickname:\u003C\/b\u003E Tendlya, Bombay Bomber, Little Master, Master Blaster \u003Cbr\u003E\u003Cb\u003EHeight:\u003C\/b\u003E 5\u00a0ft 5\u00a0in (165\u00a0cm)\u003Cbr\u003E\u003Cb\u003EBatting style:\u003C\/b\u003E Right-handed\u003Cbr\u003E\u003Cb\u003EBowling style:\u003C\/b\u003E Right-arm medium, leg break, off break\u003Cbr\u003E\u003Cb\u003ERole:\u003C\/b\u003E Batsman\u003Cbr\u003E\u003Cb\u003E\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003EInternational information\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003ENational side:\u003C\/b\u003E India\u003Cbr\u003E\u003Cb\u003ETest debut (cap\u00a0187):\u003C\/b\u003E 15 November 1989\u00a0v\u00a0Pakistan\u003Cbr\u003E\u003Cb\u003ELast Test:\u003C\/b\u003E 14 November 2013\u00a0v\u00a0West Indies\u003Cbr\u003E\u003Cb\u003EODI debut (cap\u00a074):\u003C\/b\u003E 18 December 1989\u00a0v\u00a0Pakistan\u003Cbr\u003E\u003Cb\u003ELast ODI:\u003C\/b\u003E 18 March 2012\u00a0v\u00a0Pakistan\u003Cbr\u003E\u003Cb\u003EODI shirt no.:\u003C\/b\u003E 10\u003Cbr\u003E\u003Cb\u003EOnly T20I (cap\u00a011):\u003C\/b\u003E 1 December 2006\u00a0v\u00a0South Africa\u003Cbr\u003E\u003Cb\u003E\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003EDomestic team information\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003E1988:\u003C\/b\u003E Cricket Club of India\u003Cbr\u003E\u003Cb\u003E1988\u20132013:\u003C\/b\u003E Mumbai\u003Cbr\u003E\u003Cb\u003E1992:\u003C\/b\u003E Yorkshire\u003Cbr\u003E\u003Cb\u003E2008\u20132013:\u003C\/b\u003E Mumbai Indians\u003Cbr\u003E\u003Cb\u003E2014:\u003C\/b\u003E Marylebone Cricket Club\u003Cbr\u003E\u003Cb\u003E\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003ECareer statistics\u003C\/b\u003E \u003Cbr\u003E\u003Cb\u003ECompetition \/ Test \/ ODI \/ FC \/ LA Matches:\u003C\/b\u003E 200 \/ 463 \/ 310 \/ 551\u003Cbr\u003E\u003Cb\u003ERuns scored:\u003C\/b\u003E 15,921 \/ 18,426 \/ 25,396 \/ 21,999\u003Cbr\u003E\u003Cb\u003EBatting average:\u003C\/b\u003E 53.78 \/ 44.83 \/ 57.84 \/ 45.54\u003Cbr\u003E\u003Cb\u003E100s\/50s:\u003C\/b\u003E 51\/68 \/ 49\/96 \/ 81\/116 \/ 60\/114\u003Cbr\u003E\u003Cb\u003ETop score:\u003C\/b\u003E 248* \/ 200* \/ 248* \/ 200*\u003Cbr\u003E\u003Cb\u003EBalls bowled:\u003C\/b\u003E 4,240 \/ 8,054 \/ 7,605 \/ 10,230\u003Cbr\u003E\u003Cb\u003EWickets:\u003C\/b\u003E 46 \/ 154 \/ 71 \/ 201\u003Cbr\u003E\u003Cb\u003EBowling average:\u003C\/b\u003E 54.17 \/ 44.48 \/ 61.74 \/ 42.17\u003Cbr\u003E\u003Cb\u003E5 wickets in innings:\u003C\/b\u003E 0 \/ 2 \/ 0 \/ 2\u003Cbr\u003E\u003Cb\u003E10 wickets in match:\u003C\/b\u003E 0 \/ n\/a \/ 0 \/ n\/a\u003Cbr\u003E\u003Cb\u003EBest bowling:\u003C\/b\u003E 3\/10 \/ 5\/32 \/ 3\/10 \/ 5\/32\u003Cbr\u003E\u003Cb\u003ECatches\/stumpings:\u003C\/b\u003E 115\/\u2013 \/ 140\/\u2013 \/ 186\/\u2013 \/ 175\/\u2013";

$output = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
  return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $data);

echo $output;

This will ouput a nicely formatted html version of your data.

To see the above code in actio visit php sandbox: http://sandbox.onlinephpfunctions.com/code/11db868791beff697efca2f538e3f35a7480764e

maxhb
  • 8,554
  • 9
  • 29
  • 53
  • That's way too complicated, rather tell `json_encode()` not to put useless escape sequences into the output. – Ulrich Eckhardt Dec 28 '15 at 12:03
  • @UlrichEckhardt This is not complicated. If user974 is just the comsumer of the ouput and can not alter then way it's generated then this is even a very good way to handle the situation. – maxhb Dec 28 '15 at 12:34
  • If you want the contained data, use `json_decode()`. It can handle non-ASCII characters both in the form of escape sequences or as UTF-8. If you want pretty-printed JSON, you can then encode the whole thing again using the proper flags. In summary, I'm still convinced that there's no reason to apply text processing tools like `preg_replace()` to JSON-encoded data. – Ulrich Eckhardt Dec 28 '15 at 14:08