Home  >  Article  >  Backend Development  >  PHP Regular Determining String Encoding_PHP Tutorial

PHP Regular Determining String Encoding_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:41:30909browse

Regular expression judgment for Chinese (automatic recognition of utf-8 & gbk encoding)
The following example program is a PHP regular expression program that correctly matches utf8 or gbk Chinese, and can accurately obtain the recognition of Chinese characters in different encoding conditions.

  1. $action = trim($_get[action]);
  2. if($action == "sub")
  3. {
  4. $str = $_post[dir];
  5. //if(!preg_match("/^[".chr(0xa1 )."-".chr(0xff)."a-za-z0-9_] $/",$str)) //gb2312 Chinese alphanumeric underline regular expression
  6. if(!preg_match(" /^[x{4e00}-x{9fa5}a-za-z0-9_] $/u",$str)) //utf-8 Chinese alphanumeric underline regular expression
  7. {
  8. echo "The [".$str."] you entered contains illegal characters";
  9. }
  10. else
  11. {
  12. echo "The [".$str."] you entered is completely legal and passed!";
  13. }
  14. }
  15. ?>
  16. Enter characters (Numbers, letters, Chinese characters, underline):

gbk:
preg_match("/^[".chr( 0xa1)."-".chr(0xff)."a-za-z0-9_] $/",$str); //gb2312 Chinese character alphanumeric underline regular expression

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486134.htmlTechArticleRegular judgment of Chinese (automatic recognition of utf-8 gbk encoding) The following example program is a php that correctly matches utf8 Or gbk Chinese regular expression program, which can accurately obtain the Chinese characters of different encoding situations...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn