Home > php教程 > php手册 > php 中英文混合字符串截取

php 中英文混合字符串截取

WBOY
Release: 2016-06-13 10:00:39
Original
814 people have browsed it

文章介绍一个实用的函数,我们如果用php substr来截取字符在中文上处理的很有问题,今天自己写了一个比较好的中文与英文字符截取的函数,有需要的朋友可以参考下。

 代码如下 复制代码

function smssubstr($string, $length) {
 if(strlen($string)   return $string; 
 }
 $strcut = '';
 for($i = 0; $i   $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
 }
 return $strcut;
}

for($i=1; $i  ${'smscontent'.$i} = smssubstr($message,$smsper);
 $message = str_replace(${'smscontent'.$i},"",$message);
}

好了,有需要的朋友拿去吧,原理我也不说多了,可以用就行了。

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template