2011-03-01 36 views
3

任何人都知道這是什麼以及如何解決它? “TheOMGA€在PHP中修復字符

<p>After 32 years in its former location, this popular restaurant 
    and bar moved to the bottom of the Avalon Bay Luxury residential building 
    that's just a walk from Angel Stadium. Modern and welcoming, the expansive 
    space is where fans, locals and families gather for upscale twists on classic 
    American dishes. Burgers here have a decidedly fun flair like “The OMG� 
    that is a burger surely meant for sharing (with many)--it's so huge that it's 
    served on a 14-inch bun. The restaurant also specializes in seafood with such 
    items as Chilean sea bass, mahi mahi and swordfish. The lounge also serves 
    signature drinks like the Rally Monkey Martini in tribute to the mascot 
    of the Angels.</p> 

這似乎是一個問題,因爲一個功能可按我不得不對JSON數據運行解碼,或JSON解碼將會失敗前:

function safeJSON_chars($data) { 
$aux = str_split($data); 
foreach($aux as $a) { 
    $a1 = urlencode($a); 
    $aa = explode("%", $a1); 
    foreach($aa as $v) { 
     if($v!="") { 
      if(hexdec($v)>127) { 
      $data = str_replace($a,"&#".hexdec($v).";",$data); 
      } 
     } 
    } 
} 
return $data;} 
+0

'回聲進行urlencode(ヶ輛( 'OMG'));「不知道這是你在找什麼。輸出'%26Atilde%3B%26%3B%26%3A%82%26%%3B%26%3A%%3B%26%3B%26%3B%26%3B%26%3B%26%3B% %26iquest%3B%26frac12%3B' – kjy112 2011-03-01 14:40:21

+0

你是否設法解決了這個問題呢? – 2011-03-01 14:50:54

+0

$ editorial_review = mb_convert_encoding($ value [「editorial_review」],'UTF-8','UTF-8');沒有幫助。 – MrPHP 2011-03-01 15:17:18

回答

6

看一看在此頁

Unicode-friendly PHP and MySQL

在此頁面,你會發現一個易於明確說明UTF-8編碼以及如何在您的網站中應用此功能以及一些實際示例。

您還需要確保使用UTF-8編碼(不含BOM)保存文件。

+2

最完整的答案imho。請注意,您還需要使用UTF8保存文件(不含BOM)。因爲在ISO文件中輸入特殊字符並嘗試將其顯示爲UTF8也會中斷;-) – Capsule 2011-03-01 14:45:08

+1

@Capsule:謝謝!沒錯,我會將其添加到我的答案中。 – 2011-03-01 14:46:53

1

您的字符集是否設置爲Latin1?嘗試使用UTF-8並查看是否修復了它。

+0

頁面設置爲utf-8,我的網頁瀏覽器也是如此。不知道數據是從哪裏設置的,它來自API。 – MrPHP 2011-03-01 15:13:12

2

在PHP中,你可以設置頁面字符集:

header("Content-type: text/html; charset=utf-8"); 
+0

頁面設置爲utf-8,我的網頁瀏覽器也是如此。不知道數據是從哪裏設置的,它來自API。 – MrPHP 2011-03-01 15:13:48

1

我發現這是很有幫助: str_replace() equivalent for Multi-byte string like UTF-8

然後使用它是這樣的:

$replace = array("&Scaron;"); 
$replace = array("&scaron;"); 
$search = array("Š"); 
$search = array("š"); 

$queryText= mb_replace($search, $replace, $queryText); 
+0

這是一個愚蠢的函數 - 名稱假設它是str_replace的替代品,但事實並非如此。 – Adder 2012-11-14 09:16:29

+0

'str_replace'已經是二進制安全的,這意味着你可以使用任何多字節字符串而不會有任何問題 – dav 2015-07-02 10:14:08