修复PHP中的字符

时间:2011-03-01 14:34:37

标签: php html encoding utf-8 character-encoding

任何人都知道这是什么以及如何解决它? “The OMG”

<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;}

5 个答案:

答案 0 :(得分:6)

查看此页面

Unicode-friendly PHP and MySQL

在此页面上,您将找到一个简单明了的UTF-8编码说明,以及如何在您的网站中应用此功能以及一些实际示例。

您还需要确保使用UTF-8编码(无BOM)保存文件。

答案 1 :(得分:2)

在PHP中,您可以设置页面字符集:

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

答案 2 :(得分:1)

你的角色集可能设置为Latin1吗?尝试使用UTF-8,看看是否修复它。

答案 3 :(得分:1)

这些将是Unicode utf-8中的微软引号。

答案 4 :(得分: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);