Skip to content

Instantly share code, notes, and snippets.

@mikealmond
Created June 5, 2012 20:46
Show Gist options
  • Select an option

  • Save mikealmond/2877760 to your computer and use it in GitHub Desktop.

Select an option

Save mikealmond/2877760 to your computer and use it in GitHub Desktop.
mb safe string shortener
<?php
function mb_str_shorten($content, $maxCharacters, $showDots = "&hellip;", $stopAtSpace = true, $encoding = 'UTF-8')
{
if(mb_strlen($content, $encoding) > $maxCharacters) {
if(!$stopAtSpace) {
return mb_substr($content, 0, $maxCharacters, $encoding) . $showDots;
} else {
$toReturn = mb_substr($content, 0, $maxCharacters, $encoding);
$pos = mb_strrpos($toReturn, ' ', 0, $encoding);
$toReturn = mb_substr($toReturn, 0, $pos, $encoding) . $showDots;
return $toReturn;
}
} else {
return $content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment