Skip to content

Instantly share code, notes, and snippets.

@lukemorton
Created March 22, 2011 11:48
Show Gist options
  • Save lukemorton/881105 to your computer and use it in GitHub Desktop.
Save lukemorton/881105 to your computer and use it in GitHub Desktop.
Long word shortner and trunc middle methods for Kohana_Text
<?php
class Text extends Kohana_Text {
public static function long_words($string, $max_length = 20, $elipsis = '...')
{
$words = preg_split('/\s/', $string);
$elipsis_length = strlen($elipsis);
foreach ($words as $_word)
{
if (strlen($_word) > $max_length)
{
$string = str_replace($_word, self::trunc_middle($_word, $max_length, $elipsis), $string, 1);
}
}
return $string;
}
public static function trunc_middle($string, $max_length = 20, $elipsis = '...')
{
$string_length = strlen($string);
if ($string_length > $max_length)
{
$elipsis_length = strlen($elipsis);
$buffer_length = ($max_length - $elipsis_length) / 2;
$start = substr($string, 0, $buffer_length);
$end = substr($string, $string_length - $buffer_length);
$string = "{$start}{$elipsis}{$end}";
}
return $string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment