\Greg\Support\Str
is working with strings.
- camelCase - Transform a string to
CamelCase
; - lowerCamelCase - Transform a string to
lowerCamelCase
; - snakeCase - Transform a string to
SnakE_cASe
; - lowerSnakeCase - Transform a string to
lower_snake_case
; - upperSnakeCase - Transform a string to
UPPER_SNAKE_CASE
; - upperWordsSnakeCase - Transform a string to
Upper_Words_Snake_Case
; - kebabCase - Transform a string to
KebaB-cASe
; - spinalCase - Transform a string to
spinal-case
; - trainCase - Transform a string to
Train-Case
; - lispCase - Transform a string to
Lisp-case
; - phpCamelCase - Transform a string to
PhpCamelCase
; - phpLowerCamelCase - Transform a string to
phpLowerCamelCase
; - phpSnakeCase - Transform a string to
PHP_SnakE_cASe
; - phpLowerSnakeCase - Transform a string to
php_lower_snake_case
; - phpUpperSnakeCase - Transform a string to
PHP_UPPER_SNAKE_CASE
; - phpUpperWordsSnakeCase - Transform a string to
Php_Upper_Words_Snake_Case
; - abbreviation - Transform a string to an abbreviation;
- replaceLetters - Replace letters of a string. Keep cases;
- replaceAccents - Replace accents of a string;
- is - Determine if a given string matches a given pattern;
- startsWith - Determine if a given string starts with a given substring;
- endsWith - Determine if a given string ends with a given substring;
- shift - Shift a string off from the beginning of the string;
- pop - Pop a string off from the ending of the string;
- quote - Quote a string;
- split - Split a string;
- splitPath - Split a string path;
- splitQuoted - Split quoted values in a string;
- parse - Parse a string;
- generate - Generate a cryptographically secure pseudo-random string;
- nth - Get the nth of a number;
- systemName - Transform a string to a system name;
- parseUrls - Parse URLs from a string;
- isEmpty - Determine if a variable is empty;
- isScalar - Determine if a variable is scalar;
- isDigit - Determine if a string is a digit.
Transform a string to CamelCase
.
camelCase(string $string): string
$string
- The string.
Example:
\Greg\Support\Str::camelCase('camel case'); // result: CamelCase
Transform a string to lowerCamelCase
.
lowerCamelCase(string $string): string
$string
- The string.
Example:
\Greg\Support\Str::lowerCamelCase('lower camel case'); // result: lowerCamelCase
Transform a string to SnakE_cASe
.
snakeCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::snakeCase('SnakE cASe'); // result: SnakE_cASe
Transform a string to lower_snake_case
.
lowerSnakeCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::lowerSnakeCase('Lower SnakE cASe'); // return: lower_snake_case
Transform a string to UPPER_SNAKE_CASE
.
upperSnakeCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::upperSnakeCase('Upper SnakE cASe'); // return: UPPER_SNAKE_CASE
Transform a string to Upper_Words_Snake_Case
.
upperWordsSnakeCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::upperWordsSnakeCase('Upper words SnakE cASe'); // return: Upper_Words_Snake_Case
Transform a string to KebaB-cASe
.
kebabCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::kebabCase('KebaB cASe'); // return: KebaB-cASe
Transform a string to spinal-case
.
spinalCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::spinalCase('SpinaL cASe'); // return: spinal-case
Transform a string to Train-Case
.
trainCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::trainCase('TraiN cASe'); // return: Train-Case
Transform a string to Lisp-case
.
lispCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::lispCase('LisP cASe'); // return: Lisp-case
Transform a string to PhpCamelCase
.
phpCamelCase(string $string): string
$string
- The string.
Example:
\Greg\Support\Str::phpCamelCase('1 php camel case'); // result: _1PhpCamelCase
Transform a string to phpLowerCamelCase
.
phpLowerCamelCase(string $string): string
$string
- The string.
Example:
\Greg\Support\Str::phpLowerCamelCase('1 php lower camel case'); // result: _1PhpLowerCamelCase
Transform a string to PHP_SnakE_cASe
.
phpSnakeCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::phpSnakeCase('1 PHP SnakE cASe'); // result: _1_PHP_SnakE_cASe
Transform a string to php_lower_snake_case
.
phpLowerSnakeCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::phpLowerSnakeCase('1 PHP Lower SnakE cASe'); // return: 1_php_lower_snake_case
Transform a string to PHP_UPPER_SNAKE_CASE
.
phpUpperSnakeCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::phpUpperSnakeCase('1 PHP Upper SnakE cASe'); // return: _1_PHP_UPPER_SNAKE_CASE
Transform a string to Php_Upper_Words_Snake_Case
.
phpUpperWordsSnakeCase(string $string, boolean $splitCamelCase = false): string
$string
- The string;
$splitCamelCase
- Split camel case.
Example:
\Greg\Support\Str::phpUpperWordsSnakeCase('1 PHP Upper words SnakE cASe'); // return: _1_Php_Upper_Words_Snake_Case
Transform a string to an abbreviation.
abbreviation(string $string): string
$string
- The string;
Example:
\Greg\Support\Str::abbreviation('Federal Bureau of Investigation'); // return: FBI
Replace letters of a string. Keep cases.
replaceLetters(string $string, string $search, string $replace): string
$string
- The string;
$search
- Letters;
$replace
- Replacement.
Example:
\Greg\Support\Str::replaceLetters('Înger', 'î', 'i'); // return: Inger
Replace letters of a string. Keep cases.
replaceAccents(string $string): string
$string
- The string.
Example:
\Greg\Support\Str::replaceAccents('Înghețată'); // return: Inghetata
Determine if a given string matches a given pattern.
is(string $string, string $pattern): boolean
$string
- The string;
$pattern
- String pattern.
Example:
\Greg\Support\Str::is('Foo Bar', "Foo *"); // return: true
\Greg\Support\Str::is('Foo Bar', "* Foo"); // return: false
Determine if a given string starts with a given substring.
startsWith(string $string, string|array $needles): boolean
$string
- The string;
$needles
- Needles.
Example:
\Greg\Support\Str::startsWith('Foo Bar', "Foo"); // return: true
\Greg\Support\Str::startsWith('Foo Bar', "Bar"); // return: false
Determine if a given string ends with a given substring.
endsWith(string $string, string|array $needles): boolean
$string
- The string;
$needles
- Needles.
Example:
\Greg\Support\Str::endsWith('Foo Bar', "Bar"); // return: true
\Greg\Support\Str::endsWith('Foo Bar', "Foo"); // return: false
Shift a string off from the beginning of the string.
shift(string $string, string $shift): string
$string
- The string;
$shift
- Shift string.
Example:
\Greg\Support\Str::shift('Foo Bar', "Foo "); // return: Bar
\Greg\Support\Str::shift('Foo Bar', "Baz "); // return: Bar
Pop a string off from the ending of the string.
pop(string $string, string $pop): string
$string
- The string;
$pop
- Pop string.
Example:
\Greg\Support\Str::pop('Foo Bar', " Bar"); // return: Foo
\Greg\Support\Str::pop('Foo Bar', " Baz"); // return: Foo
Quote a string.
quote(string $string, string $start = '"', string $end = null): string
$string
- The string;
$start
- Start with;
$end
- End with. If empty, uses $start
.
Example:
\Greg\Support\Str::quote('Foo Bar'); // return: "Foo Bar"
\Greg\Support\Str::quote('Foo Bar', "'"); // return: 'Foo Bar'
\Greg\Support\Str::quote('Foo Bar', '<', '>'); // return: <Foo Bar>
Split a string.
split(string $string, string $delimiter = '', int $limit = null): array
$string
- The string;
$delimiter
- Delimiter;
$limit
- Limit.
Example:
\Greg\Support\Str::split('Foo'); // return: ['F', 'o', 'o']
\Greg\Support\Str::split('Foo Bar', ' '); // return: ['Foo', 'Bar']
Split a string path.
splitPath(string $string, int $limit = null): array
$string
- The string;
$limit
- Limit.
Example:
\Greg\Support\Str::splitPath('/foo/bar'); // return: ['foo', 'bar']
Split quoted values in a string.
splitQuoted(string $string, string $delimiter = ',', string $quotes = '"'): array
$string
- The string;
$delimiter
- Delimiter;
$quotes
- Quotes.
Example:
\Greg\Support\Str::splitQuoted('"foo", "bar"'); // return: ['foo', 'bar']
Parse a string.
parse(string $string, string $delimiter = '&', string $keyValueDelimiter = '='): array
$string
- The string;
$limit
- Limit.
Example:
\Greg\Support\Str::parse('foo=1&bar=2'); // return: ['foo' => 1, 'bar' => 2]
Generate a cryptographically secure pseudo-random string.
generate(int $length, string $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'): string
$length
- Length of the string;
$characters
- Characters list.
Example:
\Greg\Support\Str::generate(6); // return: vfEvk4
\Greg\Support\Str::generate(6); // return: A6nDO1
Get the nth of a number.
nth(int $number): string
$number
- The number.
Example:
\Greg\Support\Str::nth(1); // return: 1st
\Greg\Support\Str::nth(2); // return: 2nd
\Greg\Support\Str::nth(3); // return: 3rd
Transform a string to a system name.
systemName(int $number): string
$number
- The number.
Example:
\Greg\Support\Str::systemName('Foo bar.'); // return: foo-bar
\Greg\Support\Str::systemName('Înger înghețată.'); // return: inger-inghetata
Parse URLs from a string.
parseUrls(string $string, callable(string $url, string $href): string $callable): string
$string
- The string;
$callable
- A callable to transform decorate de URL.
$url
- Original URL;
$href
- Absolute URL.
Example:
$result = parseUrls('Search on google.com', function ($url, $href) {
return '<a href="' . $href . '">' . $url . '</a>';
})
// $result: Search on <a href="http://google.com">google.com</a>
Determine if a variable is empty.
isEmpty(mixed $var): boolean
$var
- The variable.
Example:
\Greg\Support\Str::isEmpty(''); // return: true
\Greg\Support\Str::isEmpty(null); // return: true
\Greg\Support\Str::isEmpty("not empty"); // return: flase
\Greg\Support\Str::isEmpty(false); // return: flase
Determine if a variable is scalar.
isScalar(mixed $var): boolean
$var
- The variable.
Example:
\Greg\Support\Str::isScalar(''); // return: true
\Greg\Support\Str::isScalar(null); // return: true
\Greg\Support\Str::isScalar(function() {}); // return: false
Determine if a string is a digit.
isDigit(mixed $var): boolean
$var
- The variable.
Example:
\Greg\Support\Str::isDigit(1); // return: true
\Greg\Support\Str::isDigit('12'); // return: true
\Greg\Support\Str::isDigit(true); // return: true
\Greg\Support\Str::isDigit(false); // return: true
\Greg\Support\Str::isDigit('1a'); // return: false