String Helpers
Available Methods
camel_case
class_basename
e
ends_with
kebab_case
snake_case
str_limit
starts_with
str_after
str_before
str_contains
str_finish
str_handle
str_is
str_plural
str_random
str_singular
str_slug
studly_case
title_case
trans
trans_choice
Method Listing
camel_case()
The camel_case
function converts the given string to camelCase
:
$camel = camel_case('foo_bar');
// fooBar
class_basename()
The class_basename
returns the class name of the given class with the class' namespace removed:
$class = class_basename('Foo\Bar\Baz');
// Baz
e()
The e
function runs PHP's htmlspecialchars
function with the double_encode
option set to false
:
echo e('<html>foo</html>');
// <html>foo</html>
ends_with()
The ends_with
function determines if the given string ends with the given value:
$value = ends_with('This is my name', 'name');
// true
kebab_case()
The kebab_case
function converts the given string to kebab-case
:
$value = kebab_case('fooBar');
// foo-bar
snake_case()
The snake_case
function converts the given string to snake_case
:
$snake = snake_case('fooBar');
// foo_bar
str_limit()
The str_limit
function limits the number of characters in a string. The function accepts a string as its first argument and the maximum number of resulting characters as its second argument:
$value = str_limit('The PHP framework for web artisans.', 7);
// The PHP...
starts_with()
The starts_with
function determines if the given string begins with the given value:
$value = starts_with('This is my name', 'This');
// true
str_after()
The str_after
function returns everything after the given value in a string:
$value = str_after('This is: a test', 'This is:');
// ' a test'
str_before()
The str_before
function returns everything before the given value in a string:
$value = str_before('Test :it before', ':it before');
// 'Test '
str_contains()
The str_contains
function determines if the given string contains the given value:
$value = str_contains('This is my name', 'my');
// true
You may also pass an array of values to determine if the given string contains any of the values:
$value = str_contains('This is my name', ['my', 'foo']);
// true
str_finish()
The str_finish
function adds a single instance of the given value to a string if it does not already end with it:
$string = str_finish('this/string', '/');
$string2 = str_finish('this/string/', '/');
// this/string/
str_handle()
The str_handle
function generates a code friendly "handle" from the given string:
$title = str_slug('Laravel 5 Framework');
// laravel_5_framework
str_is()
The str_is
function determines if a given string matches a given pattern. Asterisks may be used to indicate wildcards:
$value = str_is('foo*', 'foobar');
// true
$value = str_is('baz*', 'foobar');
// false
str_plural()
The str_plural
function converts a string to its plural form. This function currently only supports the English language:
$plural = str_plural('car');
// cars
$plural = str_plural('child');
// children
You may provide an integer as a second argument to the function to retrieve the singular or plural form of the string:
$plural = str_plural('child', 2);
// children
$plural = str_plural('child', 1);
// child
str_random()
The str_random
function generates a random string of the specified length. This function uses PHP's random_bytes
function:
$string = str_random(40);
str_singular()
The str_singular
function converts a string to its singular form. This function currently only supports the English language:
$singular = str_singular('cars');
// car
str_slug()
The str_slug
function generates a URL friendly "slug" from the given string:
$title = str_slug('Laravel 5 Framework');
// laravel-5-framework
studly_case()
The studly_case
function converts the given string to StudlyCase
:
$value = studly_case('foo_bar');
// FooBar
title_case()
The title_case
function converts the given string to Title Case
:
$title = title_case('a nice title uses the correct case');
// A Nice Title Uses The Correct Case
trans()
The trans
function translates the given language line using Laravel's localization files:
echo trans('validation.required'):
trans_choice()
The trans_choice
function translates the given language line with inflection:
$value = trans_choice('foo.bar', $count);