rearrange str class.
This commit is contained in:
parent
ab17ea674a
commit
b73a60de2a
|
@ -123,6 +123,34 @@ public static function limit($value, $limit = 100, $end = '...')
|
||||||
return substr($value, 0, $limit).$end;
|
return substr($value, 0, $limit).$end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Limit the number of words in a string.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* // Returns "This is a..."
|
||||||
|
* echo Str::words('This is a sentence.', 3);
|
||||||
|
*
|
||||||
|
* // Limit the number of words and append a custom ending
|
||||||
|
* echo Str::words('This is a sentence.', 3, '---');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @param int $words
|
||||||
|
* @param string $end
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function words($value, $words = 100, $end = '...')
|
||||||
|
{
|
||||||
|
preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/', $value, $matches);
|
||||||
|
|
||||||
|
if (static::length($value) == static::length($matches[0]))
|
||||||
|
{
|
||||||
|
$end = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return rtrim($matches[0]).$end;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the singular form of the given word.
|
* Get the singular form of the given word.
|
||||||
*
|
*
|
||||||
|
@ -166,34 +194,6 @@ public static function plural($value, $count = 2)
|
||||||
return (ctype_upper($value[0])) ? static::title($plural) : $plural;
|
return (ctype_upper($value[0])) ? static::title($plural) : $plural;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Limit the number of words in a string.
|
|
||||||
*
|
|
||||||
* <code>
|
|
||||||
* // Returns "This is a..."
|
|
||||||
* echo Str::words('This is a sentence.', 3);
|
|
||||||
*
|
|
||||||
* // Limit the number of words and append a custom ending
|
|
||||||
* echo Str::words('This is a sentence.', 3, '---');
|
|
||||||
* </code>
|
|
||||||
*
|
|
||||||
* @param string $value
|
|
||||||
* @param int $words
|
|
||||||
* @param string $end
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function words($value, $words = 100, $end = '...')
|
|
||||||
{
|
|
||||||
preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/', $value, $matches);
|
|
||||||
|
|
||||||
if (static::length($value) == static::length($matches[0]))
|
|
||||||
{
|
|
||||||
$end = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return rtrim($matches[0]).$end;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a URL friendly "slug" from a given string.
|
* Generate a URL friendly "slug" from a given string.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue