It is valid for intercepting strings of a specified number of characters.

Source: Internet
Author: User
FunctioncsubStr ($ str, $ start, $ len) {$ strlenstrlen ($ str); $ clen0; for ($ i0; $ I $ start + $ len) break; if (ord (substr ($ str, $ I, 1) 0xa0) {if ($ clen $ start) $ tmpstr. substr ($ str, $ I, 2)


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;
}
Example: $ str = csubStr ("Hello everyone, take the string of the specified number of words, 886! ", 0, 10 );
This function is usually used to display the list of forum topics. for example, you can use the following function:
Function showShort ($ str, $ len)
{
If ($ len <(strlen ($ str)/2 ))
{
$ TempStr = csubStr ($ str, 0, $ len );
$ TempStr. = "...";
}
Else
{
$ TempStr = $ str;
}
Return $ tempStr;
}
When the topic exceeds the specified number of words, it is intercepted and a ellipsis is added. Otherwise, all words are displayed.
Eclipse replied: 21:39:31 [B] [color = red] is not very useful? [/Color] [/B]
Bigmouse replied: 12:02:54 is still useful. if you do not judge whether the Chinese byte is intercepted, garbled characters will occur when it is captured to the middle of the Chinese character. after all, the Chinese character is dubyte, make a special judgment.
The response from edevil is as follows: 11:42:07 mb_substr.


Wubai replied to: 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 to: 15:13:29 good. This Post launched well. I can use it right away. thank you very much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.