Skip to content

Instantly share code, notes, and snippets.

@matesnippets
Created March 9, 2015 15:00
Show Gist options
  • Select an option

  • Save matesnippets/0f5213bf947da220e51b to your computer and use it in GitHub Desktop.

Select an option

Save matesnippets/0f5213bf947da220e51b to your computer and use it in GitHub Desktop.
PHP, Truncate string to a given length
/**
* Cut string to lenght and add prefix
* @param string $string The string to truncate
* @param int $length The amount of characters wanted
* @param string $prefix The prefix, default ...
* @return string The modified string
*/
function str_truncater($string, $length, $prefix = '...')
{
return strlen($string) > $length ? substr($string, 0, $length).$prefix : $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment