function sub_str($str, $length = 0, $append = true) { $str = trim($str); $strlength = strlen($str); if ($length == 0 || $length >= $strlength) { return $str; //截取長度等於0或大於等於本字符串的長度,返回字符串自己 } elseif ($length < 0) //若是截取長度爲負數 { $length = $strlength + $length;//那麼截取長度就等於字符串長度減去截取長度 if ($length < 0) { $length = $strlength;//若是截取長度的絕對值大於字符串自己長度,則截取長度取字符串自己的長度 } } if (function_exists('mb_substr')) { $newstr = mb_substr($str, 0, $length, EC_CHARSET); } elseif (function_exists('iconv_substr')) { $newstr = iconv_substr($str, 0, $length, EC_CHARSET); } else { //$newstr = trim_right(substr($str, 0, $length)); $newstr = substr($str, 0, $length); } if ($append && $str != $newstr) { $newstr .= '...'; } return $newstr; }