PHP 截取字符串

11. 截取GB2312中文字符串 2 3< ?php 4 //截取中文字符串 5 function mysubstr($str, $start, $len) { 6 $tmpstr = ""; 7 $strlen = $start + $len; 8 for($i = 0; $i < $strlen; $i++) { 9 if(ord(substr($str, $i, 1)) > 0xa0) { 10 $tmpstr .= substr($str, $i, 2); 11 $i++; 12 } else 13 $tmpstr .= substr($str, $i, 1); 14 } 15 return $tmpstr; 16 } 17 ?> 182. 截取utf8编码的多字节字符串 19 20< ?php 21 //截取utf8字符串 22 function utf8Substr($str, $from, $len) 23 { 24 return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'. 25 '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s', 26 '$1',$str); 27 } 28 ?> 293. UTF-8GB2312都支持的汉字截取函数 30< ?php 31 /* 32 Utf-8、gb2312都支持的汉字截取函数 33 cut_str(字符串, 截取长度, 开始长度, 编码); 34 编码默认为 utf-8 35 开始长度默认为 0 36 */ 37 38 function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') 39 { 40 if($code == 'UTF-8') 41 { 42 $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]/"; 43 preg_match_all($pa, $string, $t_string); 44 45 if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."..."; 46 return join('', array_slice($t_string[0], $start, $sublen)); 47 } 48 else 49 { 50 $start = $start*2; 51 $sublen = $sublen*2; 52 $strlen = strlen($string); 53 $tmpstr = ''; 54 55 for($i=0; $i< $strlen; $i++) 56 { 57 if($i>=$start && $i< ($start+$sublen)) 58 { 59 if(ord(substr($string, $i, 1))>129) 60 { 61 $tmpstr.= substr($string, $i, 2); 62 } 63 else 64 { 65 $tmpstr.= substr($string, $i, 1); 66 } 67 } 68 if(ord(substr($string, $i, 1))>129) $i++; 69 } 70 if(strlen($tmpstr)< $strlen ) $tmpstr.= "..."; 71 return $tmpstr; 72 } 73 } 74 75 $str = "abcd需要截取的字符串"; 76 echo cut_str($str, 8, 0, 'gb2312'); 77 ?> 784. BugFree 的字符截取函数 79< ?php 80 /** 81 * @package BugFree 82 * @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $ 83 * 84 * 85 * Return part of a string(Enhance the function substr()) 86 * 87 * @author Chunsheng Wang <wwccss@263.net> 88 * @param string $String the string to cut. 89 * @param int $Length the length of returned string. 90 * @param booble $Append whether append "...": false|true 91 * @return string the cutted string. 92 */ 93 function sysSubStr($String,$Length,$Append = false) 94 { 95 if (strlen($String) < = $Length ) 96 { 97 return $String; 98 } 99 else 100 { 101 $I = 0; 102 while ($I < $Length) 103 { 104 $StringTMP = substr($String,$I,1); 105 if ( ord($StringTMP) >=224 ) 106 { 107 $StringTMP = substr($String,$I,3); 108 $I = $I + 3; 109 } 110 elseif( ord($StringTMP) >=192 ) 111 { 112 $StringTMP = substr($String,$I,2); 113 $I = $I + 2; 114 } 115 else 116 { 117 $I = $I + 1; 118 } 119 $StringLast[] = $StringTMP; 120 } 121 $StringLast = implode("",$StringLast); 122 if($Append) 123 { 124 $StringLast .= "..."; 125 } 126 return $StringLast; 127 } 128 } 129 130 $String = "CodeBit.cn -- 简单、精彩、通用"; 131 $Length = "18"; 132 $Append = false; 133 echo sysSubStr($String,$Length,$Append); 134 ?>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

PHP 截取字符串 - HelloWorld