Example of intercepting the length of a Chinese character using a php custom function
- 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;
- }
Call:
-
$ Str = "this character is long ,''';
- $ Short_Str = showShort ($ str, 4); // extract the first four Chinese characters and 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 ++;
- } Bbs.it-home.org
- Else
- {
- If ($ clen> = $ start)
- $ Tmpstr. = substr ($ str, $ I, 1 );
- }
- }
-
Return $ tmpstr;
- }
- Function showShort ($ str, $ len)
- {
- $ Tempstr = csubstr ($ str, 0, $ len );
- If ($ str <> $ tempstr)
- $ Tempstr. = "..."; // you can modify the description here.
-
Return $ tempstr;
- }
Here is a more concise method for intercepting the length of Chinese characters:
- $ Len = 19;
- $ Text = "how to display only the first few words in a long news title, which is replaced? ";
- Echo strlen ($ text) <= $ len? $ Text: (substr ($ text, 0, $ len). chr (0 )."....");
|