zoukankan      html  css  js  c++  java
  • PHP 截取字符串专题(转)

    1. 截取GB2312中文字符串

    1 <?php
    2  < ?php
    3 //截取中文字符串
    4 function mysubstr($str, $start, $len) {
    5 $tmpstr = "";
    6 $strlen = $start + $len;
    7 for($i = 0; $i < $strlen; $i++) {
    8 if(ord(substr($str, $i, 1)) > 0xa0) {
    9 $tmpstr .= substr($str, $i, 2);
    10 $i++;
    11 } else
    12 $tmpstr .= substr($str, $i, 1);
    13 }
    14 return $tmpstr;
    15 }
    16 ?>
    2. 截取utf8编码的多字节字符串

    1 <?php
    2 < ?php
    3 //截取utf8字符串
    4 function utf8Substr($str, $from, $len)
    5 {
    6 return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
    7 '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
    8 '$1',$str);
    9 }
    10 ?>
    3. UTF-8、GB2312都支持的汉字截取函数

    1 <?php
    2 < ?php
    3 /*
    4 Utf-8、gb2312都支持的汉字截取函数
    5 cut_str(字符串, 截取长度, 开始长度, 编码);
    6 编码默认为 utf-8
    7 开始长度默认为 0
    8 */
    9
    10 function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
    11 {
    12 if($code == 'UTF-8')
    13 {
    14 $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
    15 preg_match_all($pa, $string, $t_string);
    16
    17 if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
    18 return join('', array_slice($t_string[0], $start, $sublen));
    19 }
    20 else
    21 {
    22 $start = $start*2;
    23 $sublen = $sublen*2;
    24 $strlen = strlen($string);
    25 $tmpstr = '';
    26
    27 for($i=0; $i< $strlen; $i++)
    28 {
    29 if($i>=$start && $i< ($start+$sublen))
    30 {
    31 if(ord(substr($string, $i, 1))>129)
    32 {
    33 $tmpstr.= substr($string, $i, 2);
    34 }
    35 else
    36 {
    37 $tmpstr.= substr($string, $i, 1);
    38 }
    39 }
    40 if(ord(substr($string, $i, 1))>129) $i++;
    41 }
    42 if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
    43 return $tmpstr;
    44 }
    45 }
    46
    47 $str = "abcd需要截取的字符串";
    48 echo cut_str($str, 8, 0, 'gb2312');
    49 ?>
    4. BugFree 的字符截取函数

    1 <?php
    2 < ?php
    3 /**
    4 * @package BugFree
    5 * @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
    6 *
    7 *
    8 * Return part of a string(Enhance the function substr())
    9 *
    10 * @author Chunsheng Wang <wwccss@263.net>
    11 * @param string $String the string to cut.
    12 * @param int $Length the length of returned string.
    13 * @param booble $Append whether append "...": false|true
    14 * @return string the cutted string.
    15 */
    16 function sysSubStr($String,$Length,$Append = false)
    17 {
    18 if (strlen($String) < = $Length )
    19 {
    20 return $String;
    21 }
    22 else
    23 {
    24 $I = 0;
    25 while ($I < $Length)
    26 {
    27 $StringTMP = substr($String,$I,1);
    28 if ( ord($StringTMP) >=224 )
    29 {
    30 $StringTMP = substr($String,$I,3);
    31 $I = $I + 3;
    32 }
    33 elseif( ord($StringTMP) >=192 )
    34 {
    35 $StringTMP = substr($String,$I,2);
    36 $I = $I + 2;
    37 }
    38 else
    39 {
    40 $I = $I + 1;
    41 }
    42 $StringLast[] = $StringTMP;
    43 }
    44 $StringLast = implode("",$StringLast);
    45 if($Append)
    46 {
    47 $StringLast .= "...";
    48 }
    49 return $StringLast;
    50 }
    51 }
    52
    53 $String = "CodeBit.cn -- 简单、精彩、通用";
    54 $Length = "18";
    55 $Append = false;
    56 echo sysSubStr($String,$Length,$Append);
    57 ?>

    aliyun活动 https://www.aliyun.com/acts/limit-buy?userCode=re2o7acl
  • 相关阅读:
    进程与线程
    the art of seo(chapter seven)
    the art of seo(chapter six)
    the art of seo(chapter five)
    the art of seo(chapter four)
    the art of seo(chapter three)
    the art of seo(chapter two)
    the art of seo(chapter one)
    Sentinel Cluster流程分析
    Sentinel Core流程分析
  • 原文地址:https://www.cnblogs.com/wangbin/p/1660806.html
Copyright © 2011-2022 走看看