Created
June 29, 2010 13:43
-
-
Save kahlil/457237 to your computer and use it in GitHub Desktop.
Shorten Strings and leave the last word complete
This file contains 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
<?php | |
function shorten_string ($string, $string_max_length = 150) | |
{ | |
if (empty($string) == true) | |
{ | |
return('Es wurde kein String an die Function: shorten_string() übergeben!'); | |
} | |
else | |
{ | |
$string_length = strlen($string); | |
if ($string_length >= $string_max_length) | |
{ | |
$string = preg_replace("/[^ ]*$/", '', substr($string, 0, $string_max_length)); | |
$string .= ' ...'; | |
} | |
return ($string); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment