zoukankan      html  css  js  c++  java
  • PHP 中文字符串相关

    1、字符串长度

    中文字符串使用strlen() 获取字符串长度时一个UTF8的中文字符是3个字节长度;使用mb_strlen(),选定内码为UTF8,则会将一个中文字符当作长度1来计算

    在对含中文字符串截取时

    $str = "this你看我哪里像好人";
    
    echo substr($str,4,4);//你�
    
    echo mb_substr($str,4,4,'UTF-8');//你看我哪
    
    echo mb_strcut($str,4,4,'UTF-8');//

    2、字符串中中文验证

    a、字符串中是否含中文

    $str = "this你看我哪里像好人";
    
    $pattern = '/[x7f-xff]/'; // OR $pattern = '/[^x00-x80]/';
     
    echo preg_match($pattern, $str) ? '有' : '无';

    b、字符串中是否只含中文

    $str = "this你看我哪里像好人";
    
    if (preg_match_all("/^([x81-xfe][x40-xfe])+$/", $str, $match)) {
      echo '是';
    } else {
      echo '否';
    }
    
    if (!eregi("[^x80-xff]", "$str")) {
      echo '是';
    } else {
      echo '否';
    }
  • 相关阅读:
    HTML5 模板推荐
    OpenCV初探
    微信开放框架-UCToo
    ProFTPD 初探
    移动开发者服务平台-友盟
    线程原理理解
    JVM参数及性能调优
    GC——垃圾回收
    JVM内存模型
    php常用 随机数
  • 原文地址:https://www.cnblogs.com/PHPcoder404/p/9602884.html
Copyright © 2011-2022 走看看