Created
March 9, 2015 15:00
-
-
Save matesnippets/0f5213bf947da220e51b to your computer and use it in GitHub Desktop.
PHP, Truncate string to a given length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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