PHP中文字符串截取函数 - 开源中国社区
/**
* 中文字符串截取
* @param $str
* @param int $start @起始位置
* @param $length @截取长度
* @param string $ending @结尾符
* @return string
*/
private static function stringSubstr($str, $start=0, $length, $ending=''){
//$str = html_entity_decode($str); //实体字符转为html
$str = trim(strip_tags($str)); //去除html字符
$str = preg_replace("/\s| | /", "", $str);
$mb_str = mb_substr($str, $start, $length, 'utf-8');
if($length < method::abslength($str)){
$output = $mb_str.$ending;
}else{
$output = $mb_str;
}
return $output;
}
/**
* 可以统计中文字符串长度的函数
* @param $str @要计算长度的字符串
* @return int @计算长度类型,0(默认)表示一个中文算一个字符,1表示一个中文算两个字符
*/
private static function abslength($str){
if(empty($str)){
return 0;
}
if(function_exists('mb_strlen')){
return mb_strlen($str,'utf-8');
} else {
preg_match_all("/./u", $str, $ar);
return count($ar[0]);
}
}Copy after login
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
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Saving in R.E.P.O. Explained (And Save Files)
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
CakePHP Tutorial
1384
52
1384
52

