84669 Lernen von Personen
152542 Lernen von Personen
20005 Lernen von Personen
5487 Lernen von Personen
7821 Lernen von Personen
359900 Lernen von Personen
3350 Lernen von Personen
180660 Lernen von Personen
48569 Lernen von Personen
18603 Lernen von Personen
40936 Lernen von Personen
1549 Lernen von Personen
1183 Lernen von Personen
32909 Lernen von Personen
像上面这样(截取贴吧的),当字符长度超过固定的像素宽度时,截取像素宽度范围内的字符串,并在字符串后加省略号。mb_strlen函数只能获取字符数,而英文和中文一个字符所占的宽度不一样,不能根据字符数来确定,不知道该怎么实现?
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
那个可以通过 CSS 来控制的… http://jsfiddle.net/sc7uk5bw/
//截取字符串 function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){ if(function_exists("mb_substr")){ if($suffix){ return mb_substr($str, $start, $length, $charset); }else{ return mb_substr($str, $start, $length, $charset); } }elseif(function_exists('iconv_substr')) { if($suffix){ return iconv_substr($str,$start,$length,$charset); }else{ return iconv_substr($str,$start,$length,$charset); } } $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], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); if($suffix){ return $slice; }else{ return $slice; } }
给一个我平时用来截取字符串的方法吧,支持中英文
php// 截取字符串 public static function mix_substr($str, $len = 12, $dot = true) { $i = 0; $l = 0; $c = 0; $a = array(); while ($l < $len) { $t = substr($str, $i, 1); if (ord($t) >= 224) { $c = 3; $t = substr($str, $i, $c); $l += 2; } elseif (ord($t) >= 192) { $c = 2; $t = substr($str, $i, $c); $l += 2; } else { $c = 1; $l++; } // $t = substr($str, $i, $c); $i += $c; if ($l > $len) break; $a[] = $t; } $re = implode('', $a); if (substr($str, $i, 1) !== false) { array_pop($a); ($c == 1) and array_pop($a); $re = implode('', $a); $dot and $re .= '...'; } return $re; }
php
// 截取字符串 public static function mix_substr($str, $len = 12, $dot = true) { $i = 0; $l = 0; $c = 0; $a = array(); while ($l < $len) { $t = substr($str, $i, 1); if (ord($t) >= 224) { $c = 3; $t = substr($str, $i, $c); $l += 2; } elseif (ord($t) >= 192) { $c = 2; $t = substr($str, $i, $c); $l += 2; } else { $c = 1; $l++; } // $t = substr($str, $i, $c); $i += $c; if ($l > $len) break; $a[] = $t; } $re = implode('', $a); if (substr($str, $i, 1) !== false) { array_pop($a); ($c == 1) and array_pop($a); $re = implode('', $a); $dot and $re .= '...'; } return $re; }
那个可以通过 CSS 来控制的… http://jsfiddle.net/sc7uk5bw/
给一个我平时用来截取字符串的方法吧,支持中英文