- /**
- * Convert a string containing full-width numeric characters, letters, spaces, or '% +-()' to the corresponding half-width characters.
- *
- * @ Access public
- * @ Param string $ str string to be converted
- *
- * @ Return string $ str string after processing
- */
- Function make_semiangle ($ str)
- {
- $ Arr = array ('0' => '0', '1' => '1', '2' => '2 ', '3' => '3', '4' => '4 ',
- '5' => '5', '6' => '6', '7' => '7', '8' => '8 ', '9' => '9 ',
- 'A' => 'A', 'B' => 'B', 'c' => 'C', 'D' => 'D ', 'E' => 'e ',
- 'F' => 'F', 'G' => 'G', 'H' => 'h', 'I' => 'I ', 'j' => 'J ',
- 'K' => 'K', 'L' => 'L', 'M' => 'm', 'n' => 'N ', 'o' => 'O ',
- 'P' => 'P', 'q' => 'Q', 'R' => 'R', 's' =>'s ', 'T' => 'T ',
- 'U' => 'u', 'V' => 'V', 'W' => 'W', 'x' => 'X ', 'y' => 'y ',
- 'Z' => 'Z', 'a' => 'A', 'B' => 'B', 'c' => 'C ', 'D' => 'D ',
- 'E' => 'e', 'F' => 'F', 'G' => 'G', 'H' => 'h ', 'I' => 'I ',
- 'J' => 'J', 'K' => 'K', 'L' => 'L', 'M' =>'m ', 'n' => 'N ',
- 'O' => 'O', 'P' => 'P', 'q' => 'Q', 'R' => 'R ','s '=>'s ',
- 'T' => 'T', 'u' => 'u', 'V' => 'V', 'W' => 'W ', 'X' => 'X ',
- 'Y' => 'y', 'Z' => 'Z ',
- '(' => '(', ')' => ')', '[' => '[', ']' => ']', '[' => '[',
- ']' => ']', '<' => '[', '<' => ']', '=> '[', '"' => ']',
- ''' => '[', ''' => ']', '{' => '{', '}' => '}', '=>' <',
- '"' => ',
- '%' => '%', '+' => '+', '-' => '-', '-' => '-','~ '=> '-',
- ':' => ':','. '=>'. ',', '=>', '=>'. ',', '=> '.',
- ';' => ',','? '=> '? ','! '=> '! ','... '=>'-', ''' =>' | ',
- '"' => '"', ''' => ''', ''' => ''', '|' => '| ', 'Region' => '"',
- ''=>'', '$' => '$', '@' => '@', '#' => '#', '^' => '^', '&' => '&', '*' => '*');
-
- Return strtr ($ str, $ arr );
- }
Example 2: php Chinese truncation string
- /*
- Chinese character truncation functions supported by Utf-8 and gb2312
- Cut_str (string, truncation length, start length, encoding );
- The default encoding format is UTF-8.
- The default start length is 0.
- * Edit: bbs.it-home.org
- */
- Function cut_str ($ string, $ sublen, $ start = 0, $ code = 'utf-8 '){
- If ($ code = 'utf-8 '){
- $ Pa = "/[\ x01-\ x7f] | [\ xc2-\ xdf] [\ x80-\ xbf] | \ xe0 [\ xa0-\ xbf] [\ x80 -\ xbf] | [\ xe1-\ xef] [\ x80-\ xbf] [\ x80-\ xbf] | \ xf0 [\ x90-\ xbf] [\ x80- \ xbf] [\ x80-\ xbf] | [\ xf1-\ xf7] [\ x80-\ xbf] [\ x80-\ xbf] [\ x80-\ xbf]/ ";
- Preg_match_all ($ pa, $ string, $ t_string );
- If (count ($ t_string [0])-$ start> $ sublen)
- Return join ('', array_slice ($ t_string [0], $ start, $ sublen ))."...";
- Return join ('', array_slice ($ t_string [0], $ start, $ sublen ));
- } Else {
- $ Start = $ start * 2;
- $ Sublen = $ sublen * 2;
- $ Strlen = strlen ($ string );
- $ Tmpstr = '';
- For ($ I = 0; $ I <$ strlen; $ I ++ ){
- If ($ I >=$ start & $ I <($ start + $ sublen )){
- If (ord (substr ($ string, $ I, 1) & gt; 129 ){
- $ Tmpstr. = substr ($ string, $ I, 2 );
- } Else {
- $ Tmpstr. = substr ($ string, $ I, 1 );
- }
- }
- If (ord (substr ($ string, $ I, 1)> 129)
- $ I ++;
- }
- If (strlen ($ tmpstr) <$ strlen)
- $ Tmpstr. = "...";
- Return $ tmpstr;
- }
- }
- $ Str = "the string to be intercepted by abcd ";
- Echo cut_str ($ str, 1, 0, 'gb2312 ');
- ?>
Example 3: string loading
- /**
- * UTF-8/GBK character truncation is supported.
- * @ Param $ string
- * @ Param $ length
- * @ Param $ dot
- */
- Function str_cut ($ string, $ length, $ charset = 'utf-8', $ dot = '...'){
- $ Strlen = strlen ($ string );
- If ($ strlen <= $ length) return $ string;
- $ String = str_replace (array ('','', '&', '"', ''', '-', '<', '> ','·','... '), Array ('region', '',' & ','" ', "'", ''', ','-',' <', '> ','·','... '), $ String );
- $ Strcut = '';
- If (strtolower ($ charset) = 'utf-8 '){
- $ Length = intval ($ length-strlen ($ dot)-$ length/3 );
- $ N = $ tn = $ noc = 0;
- While ($ n <strlen ($ string )){
- $ T = ord ($ string [$ n]);
- If ($ t = 9 | $ t = 10 | (32 <= $ t & $ t <= 126 )){
- $ Tn = 1; $ n ++; $ noc ++;
- } Elseif (194 <=$ t & $ t <= 223 ){
- $ Tn = 2; $ n + = 2; $ noc + = 2;
- } Elseif (224 <=$ t & $ t <= 239 ){
- $ Tn = 3; $ n + = 3; $ noc + = 2;
- } Elseif (240 <=$ t & $ t <= 247 ){
- $ Tn = 4; $ n + = 4; $ noc + = 2;
- } Elseif (248 <=$ t & $ t <= 251 ){
- $ Tn = 5; $ n + = 5; $ noc + = 2;
- } Elseif ($ t = 252 | $ t = 253 ){
- $ Tn = 6; $ n + = 6; $ noc + = 2;
- } Else {
- $ N ++;
- }
- If ($ noc >=$ length ){
- Break;
- }
- }
- If ($ noc> $ length ){
- $ N-= $ tn;
- }
- $ Strcut = substr ($ string, 0, $ n );
- $ Strcut = str_replace (array ('hour', '&', '"'," '",'-',' <', '> ','·','... '), Array ('',' & ','" ', ''','-',' <','> ', '·','... '), $ Strcut );
- } Else {
- $ Dotlen = strlen ($ dot );
- $ Maxi = $ length-$ dotlen-1;
- $ Current_str = '';
- $ Search_arr = array ('&', '', '"', "'",'-',' <','> ', '·','... ', 'Signature ');
- $ Replace_arr = array ('&', '', '"', ''', '-', '<', '> ', '·','... ','');
- $ Search_flip = array_flip ($ search_arr );
- For ($ I = 0; $ I <$ maxi; $ I ++ ){
- $ Current_str = ord ($ string [$ I])> 127? $ String [$ I]. $ string [++ $ I]: $ string [$ I];
- If (in_array ($ current_str, $ search_arr )){
- $ Key = $ search_flip [$ current_str];
- $ Current_str = str_replace ($ search_arr [$ key], $ replace_arr [$ key], $ current_str );
- }
- $ Strcut. = $ current_str;
- }
- }
- Return $ strcut. $ dot;
- }
Example 4: Use the strrev () function to flip a character string in PHP. However, sometimes it is necessary to handle strings that contain Chinese characters, so that garbled characters will occur when strrev is used. Here, we define a function to process Chinese characters.
/**
- * Chinese string flip
- * By bbs.it-home.org
- */
- Function cstrrev ($ str)
- {
- $ Len = strlen ($ str );
- For ($ I = 0; $ I <$ len; $ I ++)
- {
- $ Char = $ str {0 };
- 'If (ord ($ char)> 127)
- {
- $ I ++;
- If ($ I <$ len)
- {
- $ Arr [] = substr ($ str, 0, 2 );
- $ Str = substr ($ str, 2 );
- }
- }
- Else
- {
- $ Arr [] = $ char;
- $ Str = substr ($ str, 1 );
- }
- }
- Return join (array_reverse ($ arr ));
- }
# Usage:
- $ Str = 'Chinese. look! ';
- Echo cstrrev ($ str );
- # Result output :! Kool.
-
Appendix 5,
- Function str_replace_cn ($ needle, $ str, $ haystack, $ charset = "UTF-8 "){
- $ Re ['utf-8'] = "/[\ x01-\ x7f] | [\ xc2-\ xdf] [\ x80-\ xbf] | [\ xe0 -\ xef] [\ x80-\ xbf] {2} | [\ xf0-\ xff] [\ x80-\ xbf] {3 }/";
- $ Re ['gb2312'] = "/[\ x01-\ x7f] | [\ xb0-\ xf7] [\ xa0-\ xfe]/";
- $ Re ['gbk'] = "/[\ x01-\ x7f] | [\ x81-\ xfe] [\ x40-\ xfe]/";
- $ Re ['big5'] = "/[\ x01-\ x7f] | [\ x81-\ xfe] ([\ x40-\ x7e] | \ xa1-\ xfe]) /";
-
- Preg_match_all ($ re [$ charset], $ haystack, $ match_haystack );
- Preg_match_all ($ re [$ charset], $ needle, $ match_needle );
-
- For ($ I = 0; $ I <count ($ match_needle); $ I ++ ){
- If (! In_array ($ match_needle [0] [$ I], $ match_haystack [0]) return $ haystack; // no matching
- }
-
- $ Match_haystack = $ match_haystack [0];
- $ Match_needle = $ match_needle [0];
-
- For ($ I = 0; $ I <count ($ match_haystack); $ I ++ ){
- If ($ match_haystack [$ I] = "") continue;
- If ($ match_haystack [$ I] = $ match_needle [0]) {
- If (count ($ match_needle) = 1) {// if only one character
- $ Match_haystack [$ I] = $ str;
- } Else {
- $ Flag = true;
- For ($ j = 1; $ j <count ($ match_needle); $ j ++ ){
- If ($ match_haystack [$ I + $ j]! = $ Match_needle [$ j]) {
- $ Flag = false;
- Break;
- }
- }
- If ($ flag) {// Match
- $ Match_haystack [$ I] = $ str;
- For ($ j = 1; $ j <count ($ match_needle); $ j ++ ){
- $ Match_haystack [$ I + $ j] = "";
- }
- }
- }
- }
- }
- Return implode ("", $ match_haystack );
- }
|