编程语言

PHP 判断字符串是否有包含乱码

By karp 3758 Views 1 MIN READ 0 Comments

判断一个字符串是否包含乱码:

  1. 可以用正则匹配,但是你需要知道乱码大概包括的符号有哪些。
  2. 运用黑魔法之json_encode($string) === 'null' 来判断。如果字符串中含有乱码,json_encode该字符串就会返回null
function isGarbled($content)
{
    $json = \json_encode((array)$content);

    return $json == false;
}

本文由 karp 原创

采用 CC BY-NC-SA 4.0 协议进行许可

转载请注明出处:https://www.ikarp.top/index.php/archives/531.html

标签: php

0 评论