Merge pull request #111 from ericbarnes/feature/str_limit
Feature/str limit
This commit is contained in:
commit
459b2165a3
|
@ -98,6 +98,55 @@ public static function length($value)
|
||||||
return strlen($value);
|
return strlen($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Limit the number of chars in a string
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* // Limit the characters
|
||||||
|
* echo Str::limit_chars('taylor otwell', 3);
|
||||||
|
* results in 'tay...'
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @param int $length
|
||||||
|
* @param string $end
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function limit($value, $length = 100, $end = '...')
|
||||||
|
{
|
||||||
|
if (static::length($value) <= $length) return $value;
|
||||||
|
|
||||||
|
if (function_exists('mb_substr'))
|
||||||
|
{
|
||||||
|
return mb_substr($value, 0, $length).$end;
|
||||||
|
}
|
||||||
|
|
||||||
|
return substr($value, 0, $length).$end;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Limit the number of words in a string
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* // Limit the words
|
||||||
|
* echo Str::limit_chars('This is a sentence.', 3);
|
||||||
|
* results in 'This is a...'
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @param int $length
|
||||||
|
* @param string $end
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function limit_words($value, $length = 100, $end = '...')
|
||||||
|
{
|
||||||
|
$count = str_word_count($value,1);
|
||||||
|
|
||||||
|
if ($count <= $length) return $value;
|
||||||
|
|
||||||
|
return implode(' ',array_slice($count,0,$length)).$end;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a string to 7-bit ASCII.
|
* Convert a string to 7-bit ASCII.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue