PHP Chinese and English mixed string interception_PHP tutorial

WBOY
Release: 2016-07-13 16:55:17
Original
750 people have browsed it

The article introduces a practical function. If we use php substr to intercept characters, it will be very problematic in Chinese. Today I wrote a better function for intercepting Chinese and English characters. Friends in need can refer to it. .

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

The code is as follows
 代码如下 复制代码

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

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

Copy code

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

Okay, friends in need can take it. I won’t go into details about the principle, just use it. http://www.bkjia.com/PHPjc/631690.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/631690.html
TechArticleThe article introduces a practical function. If we use php substr to intercept characters, it will be very problematic in Chinese. Today I wrote a better function for intercepting Chinese and English characters...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template