doufan6886 2012-04-24 11:19
浏览 147
已采纳

使用PHP中的单引号将ASCII代码转换为UTF-8

I've a problem with encoding. The output I want is:

string(17) "New & old"

This is also the output when I'm using double quotes.

This is my code. I can't change the single quotes because it's the output of another function.

$string = 'New \x26amp\x3Bamp\x3B old';
$string = iconv('ASCII', 'UTF-8//IGNORE', $string);
var_dump($string);

This is my output:

string(26) "New \x26amp\x3Bamp\x3B old"

So how can I change this behavior when using single quotes. Can you convert a string from single quotes to double quotes?

  • 写回答

1条回答 默认 最新

  • drl959975 2012-04-24 11:32
    关注

    Here's one way to do it:

    $str = preg_replace_callback('/\\\x([0-9A-F]{2})/', function ($m) {
        return pack('H*', $m[1]);
    }, $str);
    

    It's replacing hex notation with the actual byte it represents. It doesn't have much to do with Unicode.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程