PHP custom function to intercept the length of Chinese characters_PHP tutorial

WBOY
Release: 2016-07-13 10:29:39
Original
845 people have browsed it

Copy code The code is as follows:

function msubstr($str,$start,$len) {
$strlen=$start+ $len;
for($i=0;$i<$strlen;$i++) {
if(ord(substr($str,$i,1))>0xa0) {
$ tmpstr.=substr($str,$i,2);
$i++;
} else
$tmpstr.=substr($str,$i,1);
}
return $tmpstr;

}

Copy code The code is as follows:

< ;?PHP
$str="This character is so long,^_^";
$Short_Str=showShort($str,4);//Intercept the first 4 Chinese characters, the result is: this character.. .
Echo "$Short_Str";
Function csubstr($str,$start,$len)
{
$strlen=strlen($str);
$clen=0;
for($i=0;$i<$strlen;$i++,$clen++)
{
if ($clen>=$start+$len)
break;
if(ord (substr($str,$i,1))>0xa0)
{
if ($clen>=$start)
$tmpstr.=substr($str,$i,2);
$i++;
}
else
{
if ($clen>=$start)
$tmpstr.=substr($str,$i,1);
}
}

return $tmpstr;
}
Function showShort($str,$len)
{
$tempstr = csubstr($str,0,$ len);
if ($str<>$tempstr)
$tempstr .= "..."; //Whatever you want to end with, just modify it here.

return $tempstr ;
}

How about the nagging method, concise?
Copy code The code is as follows:

$len = 19;
$text = "How to display only the first few words of a long news title and replace it with...? ";
echo strlen($text)<=$len ? $text : (substr($text,0,$len).chr(0)."....");

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/770586.htmlTechArticleCopy the code The code is as follows: function msubstr($str,$start,$len) { $strlen=$start+$ len; for($i=0;$i$strlen;$i++) { if(ord(substr($str,$i,1))0xa0) { $tmpstr.=substr($str,$i,2) ; $i++; }...
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!