code为文本(PHP,语言)文本、语言、code、PHP

2023-09-10 15:46:06 作者:别像朋友.

当我提交后与AJAX不是英语,我会得到类似于%u4F60%u662F%u5982%u4F55%u505A%uFF1F 的东西 我能做些什么来解决这个问题在PHP?我试过 utf8_de code 并不起作用。

When I submit a post with AJAX that is not in English, I will get something similar to %u4F60%u662F%u5982%u4F55%u505A%uFF1F What can I do to fix this in PHP? I've tried utf8_decode and doesn't work.

我提交使用AJAX的文字是否有帮助。

I'm submitting the text with AJAX if that helps.

推荐答案

这是否你想要做什么?

<?php

  function utf8_urldecode ($str) {
    return urldecode(preg_replace_callback('/%u([0-9A-F]{4,6})/i', function($matches) {
      $int = hexdec($matches[1]);
      switch (TRUE) {
        case $int < 0x80:
          return pack('C*', $int & 0x7F);
        case $int < 0x0800:
          return pack('C*', (($int & 0x07C0) >> 6) | 0xC0, ($int & 0x3F) | 0x80);
        case $int < 0x010000:
          return pack('C*', (($int & 0xF000) >> 12) | 0xE0, (($int & 0x0FC0) >> 6) | 0x80, ($int & 0x3F) | 0x80);
        case $int < 0x110000:
          return pack('C*', (($int & 0x1C0000) >> 18) | 0xF0, (($int & 0x03F000) >> 12) | 0x80, (($int & 0x0FC0) >> 6) | 0x80, ($int & 0x3F) | 0x80);
        default:
          return $matches[0];
      }
    }, $str));
  }

  $str = "%u4F60%u662F%u5982%u4F55%u505A%uFF1F";

  echo utf8_urldecode($str);

我从来没有尝试之前,十六进制UTF-8 code点转换为二进制,原来它其实很简单,当你得到你的头周围。当然,它可能仍然显示为无稽之谈在你的浏览器,这取决于人物居然是 - 你可能需要安装语言包为他们正确呈现

I have never tried to convert hex UTF-8 code points to binary before, turns out it's actually quite easy when you get your head around it. Of course, it may still display as nonsense in your browser, depending on what the characters actually are - you may need install a language pack for them to render correctly.

 
精彩推荐
图片推荐