- /**
- * Converts a string containing full-width numeric characters, letters, spaces, or '%+-() ' characters to corresponding half-width characters
- *
- * @access Public
- * @param string $str the string to be converted
- *
- * @return string $STR processed strings
- */
- 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 ' and '-d ', ' e ' = ' e ',
- ' F ' = ' f ', ' g ' = ' g ', ' h ' = = ' h ', ' i ' = ' I ', ' j ' = ' j ',
- ' k ' = ' k ', ' 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 ' = ' = ', ' and ' = ', ' m ' + ' m ', ' n ' = ' n ',
- ' O ' = ' ' o ', ' p ' = ' P ', ' q ' = ' Q ', ' r ' = = ' R ', ' s ' = ' and ' s ',
- ' t ' = ' t ', ' u ' = ' = ' u ', ' v ' = ' = ' V ', ' w ' = ' W ', ' x ' = ' x ',
- ' Y ' = ' y ', ' z ' = ' z ',
- ' (' = ' = ' (') ' + ') ', ' (' = ' = ' [', ') ' + '] ', ' ' ' + ', ' [',
- ' ' ' = ' and ' ', ' ' = ' = ' [', ' ' ' ' = '] ', ' ' ' ' ' = ' [', ' ' '
- ' = ' + ' [', ' ' + '] ', ' {' = ' = ', '} ' = '} ', ' ' = ' < ',
- ' ' = ' > ',
- '% ' = '% ', ' + ' = ' + ', '-' = '-', '-' = '-', ' ~ ' = '-',
- ': ' = ': ', '. ' = = ', ', ', ' = = ', ', ', ' = ', ', ', ' = ', '
- '; ' = ', ', '? ' = = '? ', '! ' + '! ', ' ... ' + '-', ' ‖ ' = ' | ',
- ' ' ' = ' ', ' ' ' and ' ', ' ' and ' ', ' ' and ' ', ' | '
- ' = ' + ', ' $ ' + ' $ ', ' @ ' + ' @ ', ' # ' + ' # ', ' ^ ' + ' ^ ', ' & ' + ' & ', ' * ' = ' * ';
- Return Strtr ($str, $arr);
- }
Copy CodeExample 2,php Chinese intercept string
- /*
- Chinese character interception function supported by Utf-8 and gb2312
- Cut_str (string, intercept length, start length, encode);
- encoding defaults to Utf-8
- Start length defaults to 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)) > 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 = "ABCD string to intercept";
- Echo Cut_str ($str, 1, 0, ' gb2312 ');
- ?>
Copy CodeExample 3, the loading of a string
- /**
- * Character interception support UTF8/GBK
- * @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 (' ∵ ', ', ' & ', ' ' ', ' ' ', ' ' ', ' ' ', '-', ' < ', ' > ', ' ' ', ' ' ... '), $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 | | (<= $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 (<= $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 (', ' & ', ' "', ') , ' ' ', ' ' ' ', '-', ' < ', ' > ', ' ' ', ' ... '), $strcut);
- } else {
- $dotlen = strlen ($dot);
- $maxi = $length-$dotlen-1;
- $current _str = ";
- $search _arr = Array (' & ', ' ', ' ' ', ' ' ', ' ' ', ' ' ', '-', ' < ', ' > ', ' * ', ' ... ', ' ∵ ');
- $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;
- }
Copy CodeExample 4,php Chinese string Flip Flip a string you can use the Strrev () function. However, sometimes it is necessary to deal with the string is a Chinese language, so the use of Strrev will appear garbled. Here you can customize a function to handle characters that contain Chinese.
/**
- * Chinese string flipping
- * 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));
- }
#使用方法:
- $str = ' Chinese. look! ';
- echo Cstrrev ($STR);
- #结果输出:!kool. In the text
Copy CodeAttached 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 match
- }
- $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);
- }
Copy Code |