問題。以下是我使用需要照料一切:
function LimitText($string,$limit,$remove_html=0){
if ($remove_html==1){$string=strip_tags($string);}
$newstring = preg_replace("/(?:\s| )+/"," ",$string, -1); // replace   with space
$newstring = preg_replace(array('/\s{2,}/','/[\t\n]/'),' ',$newstring); // replace duplicate spaces
if (strlen($newstring)<=$limit) { return $newstring; } // ensure length is more than $limit
$newstring = substr($newstring,0,strrpos(substr($newstring,0,$limit),' '));
return $newstring;
}
用法:
$string = 'My wife is jealous of stackoverflow';
echo LimitText($string,20);
// My wife is jealous
使用的HTML:
$string = '<div><p>My wife is jealous of stackoverflow</p></div>';
echo LimitText($string,20,1);
// My wife is jealous
所以,你不是真的想要第28個字符,而是第28個字的長度是28。 – 2009-07-09 14:45:59
是的,我想總共28個字符..沒有一個字,如果它會返回少於28個字符沒有一個字,沒關係,這很好... – Prashant 2009-07-09 14:47:02
比確保這是明確指出的問題! – 2009-07-09 14:48:34