Skip to content

Instantly share code, notes, and snippets.

@kahlil
Created June 29, 2010 13:43
Show Gist options
  • Save kahlil/457237 to your computer and use it in GitHub Desktop.
Save kahlil/457237 to your computer and use it in GitHub Desktop.
Shorten Strings and leave the last word complete
<?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