Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php c2a0和20字符串比较_Php_Regex_Utf 8 - Fatal编程技术网

Php c2a0和20字符串比较

Php c2a0和20字符串比较,php,regex,utf-8,Php,Regex,Utf 8,我有两个utf-8字符串: 一个保存为php文件中的变量(保存在UTF-8中) 另一个使用正则表达式从另一个外部获取 当我比较这两个相同的空格分隔字符串时,结果为false,这意味着它们不一样 保存为变量的字符串I通过bin2hex(ascii编码的空格符号)呈现为20 用mb\u strtolower($string,'utf-8')处理从外部获取的字符串,用bin2hex呈现为c2a0(utf-8空格) 我的问题是: 为什么我在utf-8中保存字符串时没有完全编码为utf-8(在as

我有两个utf-8字符串:

  • 一个保存为php文件中的变量(保存在UTF-8中)
  • 另一个使用正则表达式从另一个外部获取
当我比较这两个相同的空格分隔字符串时,结果为false,这意味着它们不一样

  • 保存为变量的字符串I通过
    bin2hex
    (ascii编码的空格符号)呈现为
    20
  • mb\u strtolower($string,'utf-8')
    处理从外部获取的字符串,用
    bin2hex
    呈现为
    c2a0
    (utf-8空格)
我的问题是:

  • 为什么我在utf-8中保存字符串时没有完全编码为utf-8(在ascii中表示空格)
  • 如何解决这个问题

  • 如注释中所述,
    c2a0
    为a,
    20
    为正常

    由于可以在bin2hex中看到问题,因此可以:

    $str = hex2bin(str_replace('c2a0', '20', bin2hex($str)));
    
    或者换句话说:

    $str = preg_replace('~\xc2\a0~', ' ', $str);
    
    c2a0
    是a,而
    20
    是a。