# String Helper aka Str

  • Class with Namespace: \FluentForm\Framework\Support\Str
  • Method Types: static

# Str::camel()

The Str::camel method converts the given string to camelCase:

    use FluentForm\Framework\Support\Str;

    $converted = Str::camel('foo_bar');

    // fooBar
1
2
3
4
5

# Str::endsWith()

The Str::endsWith method determines if the given string ends with the given value:

    use FluentForm\Framework\Support\Str;

    $result = Str::endsWith('This is my name', 'name');

    // true
1
2
3
4
5

# Str::kebab()

The Str::kebab method converts the given string to kebab-case:

    use FluentForm\Framework\Support\Str;

    $converted = Str::kebab('fooBar');

    // foo-bar
1
2
3
4
5

# preg_replace_array()

The preg_replace_array function replaces a given pattern in the string sequentially using an array:

    $string = 'The event will take place between :start and :end';

    $replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string);

    // The event will take place between 8:30 and 9:00
1
2
3
4
5

# Str::snake()

The Str::snake method converts the given string to snake_case:

    use FluentForm\Framework\Support\Str;

    $converted = Str::snake('fooBar');

    // foo_bar
1
2
3
4
5

# Str::startsWith()

The Str::startsWith method determines if the given string begins with the given value:

    use FluentForm\Framework\Support\Str;

    $result = Str::startsWith('This is my name', 'This');

    // true
1
2
3
4
5

# Str::after()

The Str::after method returns everything after the given value in a string:

    use FluentForm\Framework\Support\Str;

    $slice = Str::after('This is my name', 'This is');

    // ' my name'
1
2
3
4
5

# Str::before()

The Str::before method returns everything before the given value in a string:

    use FluentForm\Framework\Support\Str;

    $slice = Str::before('This is my name', 'my name');

    // 'This is '
1
2
3
4
5

# Str::contains()

The Str::contains method determines if the given string contains the given value (case sensitive):

    use FluentForm\Framework\Support\Str;

    $contains = Str::contains('This is my name', 'my');

    // true
1
2
3
4
5

You may also pass an array of values to determine if the given string contains any of the values:

    use FluentForm\Framework\Support\Str;

    $contains = Str::contains('This is my name', ['my', 'foo']);

    // true
1
2
3
4
5

# Str::finish()

The Str::finish method adds a single instance of the given value to a string if it does not already end with the value:

    use FluentForm\Framework\Support\Str;

    $adjusted = Str::finish('this/string', '/');

    // this/string/

    $adjusted = Str::finish('this/string/', '/');

    // this/string/
1
2
3
4
5
6
7
8
9

# Str::is()

The Str::is method determines if a given string matches a given pattern. Asterisks may be used to indicate wildcards:

    use FluentForm\Framework\Support\Str;

    $matches = Str::is('foo*', 'foobar');

    // true

    $matches = Str::is('baz*', 'foobar');

    // false
1
2
3
4
5
6
7
8
9

# Str::limit()

The Str::limit method truncates the given string at the specified length:

    use FluentForm\Framework\Support\Str;

    $truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);

    // The quick brown fox...
1
2
3
4
5

You may also pass a third argument to change the string that will be appended to the end:

    use FluentForm\Framework\Support\Str;

    $truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');

    // The quick brown fox (...)
1
2
3
4
5

# Str::orderedUuid()

The Str::orderedUuid method generates a "timestamp first" UUID that may be efficiently stored in an indexed database column:

    use FluentForm\Framework\Support\Str;

    return (string) Str::orderedUuid();
1
2
3

# Str::plural()

The Str::plural method converts a string to its plural form. This function currently only supports the English language:

    use FluentForm\Framework\Support\Str;

    $plural = Str::plural('car');

    // cars

    $plural = Str::plural('child');

    // children
1
2
3
4
5
6
7
8
9

You may provide an integer as a second argument to the function to retrieve the singular or plural form of the string:

    use FluentForm\Framework\Support\Str;

    $plural = Str::plural('child', 2);

    // children

    $plural = Str::plural('child', 1);

    // child
1
2
3
4
5
6
7
8
9

# Str::random()

The Str::random method generates a random string of the specified length. This function uses PHP's random_bytes function:

    use FluentForm\Framework\Support\Str;

    $random = Str::random(40);
1
2
3

# Str::replaceArray()

The Str::replaceArray method replaces a given value in the string sequentially using an array:

    use FluentForm\Framework\Support\Str;

    $string = 'The event will take place between ? and ?';

    $replaced = Str::replaceArray('?', ['8:30', '9:00'], $string);

    // The event will take place between 8:30 and 9:00
1
2
3
4
5
6
7

# Str::replaceFirst()

The Str::replaceFirst method replaces the first occurrence of a given value in a string:

    use FluentForm\Framework\Support\Str;

    $replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');

    // a quick brown fox jumps over the lazy dog
1
2
3
4
5

# Str::replaceLast()

The Str::replaceLast method replaces the last occurrence of a given value in a string:

    use FluentForm\Framework\Support\Str;

    $replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');

    // the quick brown fox jumps over a lazy dog
1
2
3
4
5

# Str::singular()

The Str::singular method converts a string to its singular form. This function currently only supports the English language:

    use FluentForm\Framework\Support\Str;

    $singular = Str::singular('cars');

    // car

    $singular = Str::singular('children');

    // child
1
2
3
4
5
6
7
8
9

# Str::slug()

The Str::slug method generates a URL friendly "slug" from the given string:

    use FluentForm\Framework\Support\Str;

    $slug = Str::slug('Laravel 5 Framework', '-');

    // laravel-5-framework
1
2
3
4
5

# Str::start()

The Str::start method adds a single instance of the given value to a string if it does not already start with the value:

    use FluentForm\Framework\Support\Str;

    $adjusted = Str::start('this/string', '/');

    // /this/string

    $adjusted = Str::start('/this/string', '/');

    // /this/string
1
2
3
4
5
6
7
8
9

# Str::studly()

The Str::studly method converts the given string to StudlyCase:

    use FluentForm\Framework\Support\Str;

    $converted = Str::studly('foo_bar');

    // FooBar
1
2
3
4
5

# Str::title()

The Str::title method converts the given string to Title Case:

    use FluentForm\Framework\Support\Str;

    $converted = Str::title('a nice title uses the correct case');

    // A Nice Title Uses The Correct Case
1
2
3
4
5

If the specified translation key does not exist, the trans function will return the given key. So, using the example above, the trans function would return messages.welcome if the translation key does not exist.

# Str::uuid()

The Str::uuid method generates a UUID (version 4):

    use FluentForm\Framework\Support\Str;

    return (string) Str::uuid();
1
2
3