This article mainly introduces the PHP interception string length function. Interested friends can refer to it. I hope it will be helpful to everyone.
First enter the string for testing:
<?php header("Content-Type:text/html;charset=utf-8"); echo cn_substr_utf8('我是一个,和哈,哦也,,国家!',12); echo '<br />',cn_substr_utf8('ai\'2145m a ch3我[是一,个,和哈,哦也,,国家!',12); echo '<br />',cn_substr_utf8('【我,是一,个,和哈,哦也,,国家!',12); echo '<br />',cn_substr_utf8('我是一,个,和哈,哦也,,国家!',12); echo '<br />',cn_substr_utf8('我是,一,个,和哈,哦也,,国家!',12); echo '<br />',cn_substr_utf8('我,是,一,个,和哈,哦也,,国家!',12); echo '<br />',cn_substr_utf8('我是asd一,个,和哈,哦也,,国家!',12); echo '<br />',cn_substr_utf8('【我i\'m[是一,个,和哈,哦也,,国家!',12); echo '<br />',cn_substr_utf8('【i\'m a ch我[是一,个,和哈,哦也,,国家!',12); echo '<br />',cn_substr_utf8('【i\'2145m a ch3我[是一,个,和哈,哦也,,国家!',12);
The following is the rendering of accurately intercepting the string:
The specific function code is as follows :
//utf-8中文截取,单字节截取模式 function cn_substr_utf8($str,$length,$append='...',$start=0){ if(strlen($str)<$start+1){ return ''; } preg_match_all("/./su",$str,$ar); $str2=''; $tstr=''; //www.phpernote.com for($i=0;isset($ar[0][$i]);$i++){ if(strlen($tstr)<$start){ $tstr.=$ar[0][$i]; }else{ if(strlen($str2)<$length + strlen($ar[0][$i])){ $str2.=$ar[0][$i]; }else{ break; } } } return $str==$str2?$str2:$str2.$append; }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
Detailed explanation of methods and examples of PHP file upload class
Methods and examples of PHP implementation of paging class
How to generate QR code with logo in php
The above is the detailed content of About php interception string length function. For more information, please follow other related articles on the PHP Chinese website!