Home  >  Article  >  Backend Development  >  Intercepting a string with a specified number of characters is valid in Chinese_PHP tutorial

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

WBOY
WBOYOriginal
2016-07-13 17:28:06946browse


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...
Statement:
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