Php json_编码返回Unicode和拉丁字符

Php json_编码返回Unicode和拉丁字符,php,json,unicode,Php,Json,Unicode,在PHP中使用json_encode编码之后,我想要漂亮的json,并希望从字符串或json中删除Unicode和拉丁字符。我想要这个api,需要干净的响应 下面是json_编码后的unicoded json 例如 输出应为: Information about 'Sachin Tendulkar' Sachin Ramesh Tendulkar (/ˌsətʃɪn tɛnˈduːlkər/; born 24 April 1973) is a former Indian cricketer an

在PHP中使用json_encode编码之后,我想要漂亮的json,并希望从字符串或json中删除Unicode和拉丁字符。我想要这个api,需要干净的响应

下面是json_编码后的unicoded json

例如

输出应为:

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/–"

您必须将嵌入的unicode转义序列转换回相应的字符

$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;
这将输出一个格式良好的html版本的数据


要在actio中查看上述代码,请访问php沙盒:

输入是什么?另外,您要向编码器添加哪些选项?json_encode有一个选项可以做到这一点,请参见json的定义UTF-8,它是Unicode,反过来又是一个干净的响应。使用它或使用不同的元格式。换句话说,你的问题没有多大意义,我不明白你认为自己哪里有问题。这太复杂了,告诉json_encode不要在输出中放入无用的转义序列。@UlrichEckhardt这并不复杂。如果user974只是输出的使用者,并且不能改变它的生成方式,那么这甚至是处理这种情况的一种非常好的方法。如果您想要包含的数据,请使用json_decode。它可以处理转义序列形式或UTF-8形式的非ASCII字符。如果您想要打印精美的JSON,那么可以使用适当的标志再次对整个内容进行编码。总之,我仍然相信没有理由将preg_replace等文本处理工具应用于JSON编码的数据。
$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;