Skip to content

Instantly share code, notes, and snippets.

@daniloparrajr
Created February 16, 2018 03:11
Show Gist options
  • Save daniloparrajr/9bb969816c9c46f4174ac28511f51de9 to your computer and use it in GitHub Desktop.
Save daniloparrajr/9bb969816c9c46f4174ac28511f51de9 to your computer and use it in GitHub Desktop.
Truncate a string provided by the maximum limit
<?php
/**
* Truncate a string provided by the maximum limit.
* @param string $str
* @param integer $maxlen
* @return string
*/
function limitString($string, $limit = 100, $end = '&hellip;')
{
if (mb_strwidth($string, 'UTF-8') <= $limit) {
return $string;
}
return rtrim(mb_strimwidth($string, 0, $limit, '', 'UTF-8')).$end;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment