String Functions In Laravel

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023

categoryLaravel

In this tutorial we will learn string function in laravel.

 

String functions In laravel

Laravel is a popular PHP web framework that provides a variety of built-in functions and methods for working with strings.

Here are some commonly used string functions in Laravel:

 

1) str_replace()

This function is used to replace a substring with another substring within a string.

Syntax: str_replace($search, $replace, $subject)

Example: $new_string = str_replace('old', 'new', 'This is an old string');

 

2) strtolower()

This function is used to convert a string to lowercase.

Syntax: strtolower($string)

Example: $lowercase_string = strtolower('THIS IS AN UPPERCASE STRING');

 

3) strtoupper()

This function is used to convert a string to uppercase.

Syntax: strtoupper($string)

Example: $uppercase_string = strtoupper('this is a lowercase string');

 

4) ucfirst()

This function is used to capitalize the first letter of a string.

Syntax: ucfirst($string)

Example: $capitalized_string = ucfirst('this is a sentence');

 

5) ucwords()

This function is used to capitalize the first letter of each word in a string.

Syntax: ucwords($string)

Example: $capitalized_words_string = ucwords('this is a sentence');

 

6) str_limit()

This function is used to limit the number of characters in a string.

Syntax: str_limit($string, $limit)

Example: $limited_string = str_limit('This is a long string', 10);

 

7) str_contains()

This function is used to check if a string contains a specific substring.

Syntax: str_contains($string, $substring)

Example: $contains = str_contains('This is a string', 'is a');

 

8) str_starts_with()

This function is used to check if a string starts with a specific substring.

Syntax: str_starts_with($string, $substring)

Example: $starts_with = str_starts_with('This is a string', 'This');

 

9) str_ends_with()

This function is used to check if a string ends with a specific substring.

Syntax: str_ends_with($string, $substring)

Example: $ends_with = str_ends_with('This is a string', 'string');

 

10) str_plural()

This function is used to pluralize a string based on the count.

Syntax: str_plural($string, $count)

Example: $pluralized_string = str_plural('apple', 3);

 

11) str_singular()

This function is used to singularize a string.

Syntax: str_singular($string)

Example: $singular_string = str_singular('apples');

 

12) str_random()

This function is used to generate a random string of a specified length.

Syntax: str_random($length)

Example: $random_string = str_random(10);

 

13) str_slug()

This function is used to convert a string to a URL friendly slug.

Syntax: str_slug($string)

Example: $slug = str_slug('This is a URL friendly string');

 

14) str_replace_array()

This function is used to replace multiple substrings with an array of replacements in a string.

Syntax: str_replace_array($search, $replace, $subject)

Example: $new_string = str_replace_array(['old', 'new'], ['new', 'old'], 'This is an old string');

 

15) str_contains_all()

This function is used to check if a string contains all of the given substrings.

Syntax: str_contains_all($string, $substrings)

Example: $contains_all = str_contains_all('This is a string', ['is', 'string']);

 

16) str_before()

This function is used to get the part of a string before a specific substring.

Syntax: str_before($string, $substring)

Example: $before_string = str_before('This is a string', 'is');

 

17) str_after()

This function is used to get the part of a string after a specific substring.

Syntax: str_after($string, $substring)

Example: $after_string = str_after('This is a string', 'a ');

 

18) str_limit_words()

This function is used to limit the number of words in a string.

Syntax: str_limit_words($string, $limit)

Example: $limited_string = str_limit_words('This is a long string with many words', 3);

 

19) str_pad()

This function is used to pad a string with a specific character or substring to a specified length.

Syntax: str_pad($string, $length, $pad_string, $pad_type)

Example: $padded_string = str_pad('123', 5, '0', STR_PAD_LEFT);

 

20) str_shuffle()

This function is used to shuffle the characters in a string.

Syntax: str_shuffle($string)

Example: $shuffled_string = str_shuffle('This is a string');

Comments 0

Leave a comment