Commit 7b796343 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Don't chop utf-8 strings in the middle of a character

parent 8630f436
......@@ -2367,11 +2367,11 @@ function str_shorten($str, $maxlen=100, $truncate=false, $newlines=true) {
$str = html_entity_decode($str); // no things like   only take up one character
// take the first $length chars, then up to the first space (max length $length + $extra chars)
if ($truncate && strlen($str) > $maxlen) {
$str = substr($str, 0, $maxlen-3) . '...';
if ($truncate && mb_strlen($str, 'UTF-8') > $maxlen) {
$str = mb_substr($str, 0, $maxlen-3, 'UTF-8') . '...';
}
if (strlen($str) > $maxlen) {
$str = substr($str, 0, floor($maxlen / 2) - 1) . '...' . substr($str, -(floor($maxlen / 2) - 2));
if (mb_strlen($str) > $maxlen) {
$str = mb_substr($str, 0, floor($maxlen / 2) - 1, 'UTF-8') . '...' . mb_substr($str, -(floor($maxlen / 2) - 2), mb_strlen($str, 'UTF-8'), 'UTF-8');
}
$str = nl2br(hsc($str));
// this should be ok, because the string gets checked before going into the database
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment