Functions for intercepting Chinese characters (UTF-8 format) in php
/**
- @ UTF-8 Chinese character truncation program
- @ Http://bbs.it-home.org
- */
- $ Str = "321 This is a test string ";
- $ Str1 = "()()";
- Echo subUTF8str ($ str, 0, 3 )."
";
- Echo subUTF8str ($ str, 0, 4 )."
";
- Echo subUTF8str ($ str1, 0, 4 )."
";
- Echo subUTF8str ($ str1, 0, 10 )."
";
Function subUTF8str ($ str, $ start = 0, $ length = 80 ){
- $ Cur_len = 0; // string length understood by a person
- $ All_len = strlen ($ str); // machine understanding string length
- If ($ length> $ all_len)
- {
- Return $ str;
- }
- For ($ I = 0; $ I <$ all_len ;)
- {
- If ($ cur_len = $ start)
- {
- Break;
- }
- If (ord ($ str [$ I])> 127)
- {
- $ I + = 3;
- } Else {
- $ I + = 1;
- }
- $ Cur_len ++;
- }
- $ Start_pos = $ I;
- $ Temp_pos = $ cur_len;
- For (; $ cur_len-$ temp_pos <$ length ;)
- {
- If ($ I >=$ all_len)
- Break;
- If (ord ($ str [$ I])> 127)
- {
- $ I + = 3;
- } Else {
- $ I + = 1;
- }
- $ Cur_len ++;
- }
- $ End_pos = $ I;
- Return substr ($ str, $ start_pos, $ end_pos );
- }
- ?>
-
Php comes with a lot of function libraries that process strings. we suggest you study them in depth and practice them a lot. |