【问题标题】:How do I split text with strange characters?如何拆分带有奇怪字符的文本?
【发布时间】:2019-12-01 13:17:04
【问题描述】:

我想在斜杠 (//) 上拆分用户输入的 URL 的一部分。我尝试的一切都只返回 false 或空字符串。我该怎么做?

mb_internal_encoding("UTF-8");
$url = urldecode('aaa%BAbbb//ccc%D0ddd');
echo 'url: ' . $url . "\n";
$test = mb_split('//', $url);
$test2 = explode("//", $url);
$test3 = mb_ereg("//", $url);

echo "test: " . json_encode($test) . "\n-----------\n";
echo "test2: " . json_encode($test2) . "\n------------\n";
echo "test3: " . json_encode($test3) . "\n------------\n";

https://3v4l.org/a252W

谢谢

【问题讨论】:

  • 因为这些字节序列从一开始就不是有效的 UTF-8,json_encode 在这里返回 false - 但这并不意味着其余的不起作用。停止使用 json_encode 作为调试工具(?),将 var_dump($url, $test, $test2, $test3); 放在脚本的末尾,然后……瞧。
  • 看来问题出在json_encode(),因为如果你这样做print_r($test),它会返回正确的数组。

标签: php regex split explode


【解决方案1】:

explode() 有效,而 json_encode 无法输出结果。 var_dump 和 print_r 用于输出结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多