【问题标题】:Convert string in php [duplicate]在php中转换字符串[重复]
【发布时间】:2013-11-12 14:53:51
【问题描述】:

我有一个字符串:

"Hello\u00c2\u00a0World"

我想转换:

"Hello World"

我试试:

str_replace("\u00c2\u00a0"," ","Hello\u00c2\u00a0World");

str_replace("\\u00c2\\u00a0"," ","Hello\u00c2\u00a0World");

但不行!

【问题讨论】:

  • 那个字符串是从哪里来的?它可能是 JSON 编码的字符串吗?我在这里闻到了一些严重的编码转换问题。
  • 请记住,您必须将其分配回字符串,即$string = str_replace(..); - 工作:codepad.org/qcCtedUr

标签: php string str-replace


【解决方案1】:

解决!

str_replace(chr(194).chr(160)," ","Hello\u00c2\u00a0World");

【讨论】:

    【解决方案2】:

    如果您想删除 \u.... 之类的模式,则可以使用以下示例:

    $string = preg_replace('/(\\\u....)+/',' ',$input);
    

    【讨论】:

    【解决方案3】:

    你是大部分的路。

    $stuff = "Hello\u00c2\u00a0World";
    $newstuff = str_replace("\u00c2\u00a0"," ",$stuff);
    

    您需要将 str_replace 的返回值放入一个变量中,以便稍后对其进行处理。

    【讨论】:

      【解决方案4】:

      这应该可以工作

      $str="Hello\u00c2\u00a0World";   
      echo str_replace("\u00c2\u00a0"," ",$str);
      

      【讨论】:

        【解决方案5】:

        你可以试试this,取自this answer

        function replaceUnicode($str) {
            return preg_replace_callback("/\\\\u00([0-9a-f]{2})/", function($m){ return chr(hexdec($m[1])); }, $str);
        }
        echo replaceUnicode("Hello\u00c2\u00a0World");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          • 2015-09-25
          • 1970-01-01
          • 2016-03-17
          • 2014-07-16
          • 2019-09-01
          • 2016-12-24
          相关资源
          最近更新 更多