Regex to detect Invalid UTF-8 String

前端 未结 4 1414
挽巷
挽巷 2020-11-29 02:13

In PHP, we can use mb_check_encoding() to determine if a string is valid UTF-8. But that\'s not a portable solution as it requires the mbstring extension to be compiled in a

4条回答
  •  遥遥无期
    2020-11-29 02:31

    this works for me for detecting unicode characters, linke emoji or russian or chinese:

    private function has_unicode( $string ) 
    {     
        $pattern = '/^.*[^\x{00}-\x{00FF}]+.*$/u';
        return preg_match( $pattern, $string ) ? true : false;
    }
    

提交回复
热议问题