search
Homephp教程php手册字符串截取函数(支持中英文混体)

字符串截取函数(支持中英文混体)

Jun 13, 2016 am 11:18 AM
phpfunctionstringusinterceptsupportofBring your own

字符串截取函数(支持中英文混体)以前我们截取字符串都会用php自带的函数,今天我来看一下一款字符串截取函数(支持中英文混体)的php代码实例吧。  

字符串截取函数(支持中英文混体)
以前我们截取字符串都会用php教程自带的函数,今天我来看一下一款字符串截取函数(支持中英文混体)的php代码实例吧。
*/

function cutstr($string, $sublen=10, $start = 0, $code = 'utf-8')
{
 if($code == 'utf-8')
 {
 $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]/";
 preg_match_all($pa, $string, $t_string);

 //if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
 return join('', array_slice($t_string[0], $start, $sublen));
 }
 else
 {
 $start = $start*2;
 $sublen = $sublen*2;
 $strlen = strlen($string);
 $tmps教程tr = '';
 for($i=0; $i  {
 if($i>=$start && $i  {
 if(ord(substr($string, $i, 1))>129) $tmpstr.= substr($string, $i, 2);
 else $tmpstr.= substr($string, $i, 1);
 }
 if(ord(substr($string, $i, 1))>129) $i++;
 }
 //if(strlen($tmpstr)  return $tmpstr;
 }
}


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.