Intercepting a string with a specified number of characters is valid in Chinese_PHP tutorial

WBOY
Release: 2016-07-13 17:28:06
Original
933 people have browsed it


function csubStr($str,$start,$len)
{
$strlen=strlen($str);
$clen=0;
for($i=0;$ i{
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;
}
Example: $str = csubStr("Hello everyone, intercept the string with the specified number of characters, 886!",0,10);
can usually be used to display the forum topic list, such as with The following function:
function showShort($str,$len)
{
if($len {
$tempStr = csubStr( $str,0,$len);
$tempStr .= "...";
}
else
{
$tempStr =$str;
}
return $tempStr;
}
If the topic exceeds the specified number of words, it will be intercepted and then added with ellipses. Otherwise, the entire word count is displayed.
eclipse replied on: 2001-11-24 21:39:31 [b][color=red] Not much use, right?[/color][/b]
bigmouse replied on: 2001-11-29 12:02:54 It is still useful. If you intercept Chinese characters without judging them, garbled characters will appear when you intercept the middle of the Chinese characters. After all, Chinese characters are double bytes, so you need to make a special judgment.
evil replied at: 2002-11-12 11:42:07 mb_substr can do it


wubai replied at: 2002-11-12 11:49:03
Let’s take a look at this~ ~~~~
http://www.chinaunix.net/cgi-bin/bbs/topic.cgi?forum=15&topic=351&show=0
windlike replied on: 2003-05-08 15:13: 29 Okay, this post was created well. I can use it right away. It’s great. Thank you all

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531809.htmlTechArticlefunction csubStr($str,$start,$len) { $strlen=strlen($str); $clen =0; for($i=0;$i { if ($clen>=$start+$len) break; if(ord(substr($str,$i,1))>0xa0) { if ($clen> =$start) $tmpstr.=subst...
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!