http://www.nowamagic.net/php/php_SubMixedString.php
http://waiting.iteye.com/blog/581888
Function get_word ($string, $length, $dot = ' ... ', $charset = ' GBK ') { if (strlen ($string) <= $length) { return $string; } $string = str_replace (", ' ', ' & ', '" ', ' < ', ' > '), array (', ', ' & ', ' ', ' < ', ' > '), $string); $strcut = '; if (Strtolower ($charset) == ' utf-8 ') { $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 { & NBsp; $n ++; } if ($noc >= $length) { break; } } if ($noc > $length) { $n -= $tn; } $strcut = substr ($string, 0, $n); } else { for ($i = 0; $i < $length; $i + +) { $strcut .= ord ($string [$i]) > 127 ? $string [$i] . $string [+ + $i] : $string [$i]; } } return $strcut. $dot;} $str = "Welcome visit concise nowamagic"; $str _result = get_word ($str, 12); Echo $STR _result;
/** * intercept UTF8 encoded string from the first byte to specify the width (not the length), is suitable for the string length is limited, such as the news headline's equal width interception * The Chinese and English mixed the situation is more ideal . All Chinese and English after the interception of the comparison shows the width of the largest difference, and the larger the width of the interception is obvious . * @param string $strUTF -8 encoding * @ param int[option] $width Intercept width * @param string[option] $end appended trailing characters after interception * @param float[option] $x 3<p> * 3 Byte (Chinese) character equivalent to the Greek letter width coefficient coefficient (fractional) * Chinese is usually fixed in Arial, depending on the ASCII character font width setting, different browsers may have different display effects </p> * * @return string * @author waiting * http://waiting.iteye.com */function u8_title_substr ($STR, $width = 0, $end = ' ... ', $x 3 = 0) {global $CFG; Global variable Save x3 value if ($width <= 0 | | $width >= strlen ($str)) {return $str;} $arr = str_split ($STR), $len = count ($arr), $w = 0; $width *= 10;// Different byte seriesCode character width factor $x1 = 11;// ascii$x2 = 16; $x 3 = $x 3===0 ? ( $CFG [' CF3 '] > 0 ? $CFG [' CF3 ']*10 : $x 3 = 21 ] : $x 3*10; $x 4 = $x 3;// http://zh.wikipedia.org/zh-cn/utf8for ($i = 0; $i < $len; $i + +) {if ($w >= $width) {$e = $end; $c = ord ($arr [$i]);if ($c <= 127) {$w += $x 1;} elseif ($c >= 192 && $c <= 223) {// 2 byte header $w += $x 2; $i += 1;} elseif ($c >= 224 && $c <= 239) {// 3 byte header $w += $x 3; $i += 2;} elseif ($c >= 240 && $c <= 247) {// 4 byte header $w += $x 4; $i += 3;}} Return implode (", array_slice ($arr, 0, $i) ). $e;}
Utf-8 Chinese and English string interception