Added methods for Str

This commit is contained in:
huangdijia 2021-09-04 22:40:50 +08:00 committed by 李铭昕
parent 5a6ebf6c2c
commit 1a4f483f5c
2 changed files with 214 additions and 20 deletions

View File

@ -66,6 +66,28 @@ class Str
return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
}
/**
* Return the remainder of a string after the last occurrence of a given value.
*
* @param string $subject
* @param string $search
* @return string
*/
public static function afterLast($subject, $search)
{
if ($search === '') {
return $subject;
}
$position = strrpos($subject, (string) $search);
if ($position === false) {
return $subject;
}
return substr($subject, $position + strlen($search));
}
/**
* Transliterate a UTF-8 value to ASCII.
*
@ -100,6 +122,45 @@ class Str
return $search === '' ? $subject : explode($search, $subject)[0];
}
/**
* Get the portion of a string before the last occurrence of a given value.
*
* @param string $subject
* @param string $search
* @return string
*/
public static function beforeLast($subject, $search)
{
if ($search === '') {
return $subject;
}
$pos = mb_strrpos($subject, $search);
if ($pos === false) {
return $subject;
}
return static::substr($subject, 0, $pos);
}
/**
* Get the portion of a string between two given values.
*
* @param string $subject
* @param string $from
* @param string $to
* @return string
*/
public static function between($subject, $from, $to)
{
if ($from === '' || $to === '') {
return $subject;
}
return static::beforeLast(static::after($subject, $from), $to);
}
/**
* Convert a value to camel case.
*
@ -133,6 +194,24 @@ class Str
return false;
}
/**
* Determine if a given string contains all array values.
*
* @param string $haystack
* @param string[] $needles
* @return bool
*/
public static function containsAll($haystack, array $needles)
{
foreach ($needles as $needle) {
if (! static::contains($haystack, $needle)) {
return false;
}
}
return true;
}
/**
* Determine if a given string ends with a given substring.
*
@ -272,6 +351,81 @@ class Str
return rtrim($matches[0]) . $end;
}
/**
* Get the string matching the given pattern.
*
* @param string $pattern
* @param string $subject
* @return string
*/
public static function match($pattern, $subject)
{
preg_match($pattern, $subject, $matches);
if (! $matches) {
return '';
}
return $matches[1] ?? $matches[0];
}
/**
* Get the string matching the given pattern.
*
* @param string $pattern
* @param string $subject
* @return \Illuminate\Support\Collection
*/
public static function matchAll($pattern, $subject)
{
preg_match_all($pattern, $subject, $matches);
if (empty($matches[0])) {
return collect();
}
return collect($matches[1] ?? $matches[0]);
}
/**
* Pad both sides of a string with another.
*
* @param string $value
* @param int $length
* @param string $pad
* @return string
*/
public static function padBoth($value, $length, $pad = ' ')
{
return str_pad($value, $length, $pad, STR_PAD_BOTH);
}
/**
* Pad the left side of a string with another.
*
* @param string $value
* @param int $length
* @param string $pad
* @return string
*/
public static function padLeft($value, $length, $pad = ' ')
{
return str_pad($value, $length, $pad, STR_PAD_LEFT);
}
/**
* Pad the right side of a string with another.
*
* @param string $value
* @param int $length
* @param string $pad
* @return string
*/
public static function padRight($value, $length, $pad = ' ')
{
return str_pad($value, $length, $pad, STR_PAD_RIGHT);
}
/**
* Parse a Class@method style callback into class and method.
*
@ -320,6 +474,18 @@ class Str
return $string;
}
/**
* Repeat the given string.
*
* @param string $string
* @param int $times
* @return string
*/
public static function repeat(string $string, int $times)
{
return str_repeat($string, $times);
}
/**
* Replace a given value in the string sequentially with an array.
*/
@ -332,6 +498,19 @@ class Str
return $subject;
}
/**
* Replace the given value in the given string.
*
* @param string|string[] $search
* @param string|string[] $replace
* @param string|string[] $subject
* @return string
*/
public static function replace($search, $replace, $subject)
{
return str_replace($search, $replace, $subject);
}
/**
* Replace the first occurrence of a given value in the string.
*/
@ -364,6 +543,23 @@ class Str
return $subject;
}
/**
* Remove any occurrence of the given string in the subject.
*
* @param string|array<string> $search
* @param string $subject
* @param bool $caseSensitive
* @return string
*/
public static function remove($search, $subject, $caseSensitive = true)
{
$subject = $caseSensitive
? str_replace($search, '', $subject)
: str_ireplace($search, '', $subject);
return $subject;
}
/**
* Begin a string with a single instance of a given value.
*/
@ -487,6 +683,24 @@ class Str
return mb_substr($string, $start, $length, 'UTF-8');
}
/**
* Returns the number of substring occurrences.
*
* @param string $haystack
* @param string $needle
* @param int $offset
* @param int|null $length
* @return int
*/
public static function substrCount($haystack, $needle, $offset = 0, $length = null)
{
if (! is_null($length)) {
return substr_count($haystack, $needle, $offset, $length);
} else {
return substr_count($haystack, $needle, $offset);
}
}
/**
* Make a string's first character uppercase.
*/

View File

@ -274,16 +274,6 @@ class Stringable implements JsonSerializable
return Str::is($pattern, $this->value);
}
/**
* Determine if a given string is 7 bit ASCII.
*
* @return bool
*/
public function isAscii()
{
return Str::isAscii($this->value);
}
/**
* Determine if the given string is empty.
*
@ -347,16 +337,6 @@ class Stringable implements JsonSerializable
return new static(Str::lower($this->value));
}
/**
* Convert GitHub flavored Markdown into HTML.
*
* @return static
*/
public function markdown(array $options = [])
{
return new static(Str::markdown($this->value, $options));
}
/**
* Get the string matching the given pattern.
*