修复 PHP 中的字符

发布于 2024-10-19 12:03:29 字数 1216 浏览 2 评论 0原文

有人知道这是什么以及如何解决它吗? “天哪”

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

Anyone know what this is and how to fix it? “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>

This seems to a problem because of a function I have to run on the JSON data before decoding it or JSON decoding will fail:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ぺ禁宫浮华殁 2024-10-26 12:03:30

看看这个页面

Unicode 友好的 PHP 和 MySQL

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

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

Have a look at this page

Unicode-friendly PHP and MySQL

On this page you will find an easy and clear explaination of UTF-8 encoding and how to apply this in your websites along with some practical examples.

You will also need to make sure that your files are saved using UTF-8 encoding (without BOM).

夏末 2024-10-26 12:03:30

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

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

In PHP, you can set the page charset:

header("Content-type: text/html; charset=utf-8");
染年凉城似染瑾 2024-10-26 12:03:30

您的字符集是否设置为 Latin1?尝试使用 UTF-8 并查看是否可以解决问题。

Is your character-set set to Latin1 perhaps? Try using UTF-8 and see if that fixes it.

童话 2024-10-26 12:03:30

这些将是 Unicode utf-8 中的 microsoft 引用。

Those would be microsoft quotes in Unicode utf-8.

怀中猫帐中妖 2024-10-26 12:03:30

我发现这很有帮助:
str_replace() 等效项对于像 UTF-8 这样的多字节字符串

然后像这样使用它:

$replace = array("Š");
$replace = array("š");
$search = array("Š"); 
$search = array("š");  

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

I found this helpful:
str_replace() equivalent for Multi-byte string like UTF-8

Then use it like this:

$replace = array("Š");
$replace = array("š");
$search = array("Š"); 
$search = array("š");  

$queryText= mb_replace($search, $replace, $queryText);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文