Illuminate\Support\Str

Laravel: String manipulations with 'Str' class

Published on

Laravel: String manipulations with 'Str' class
Laravel: String manipulations with 'Str' class https://programiz.com

Laravel already provides powerfull helpers for string manipulations, but I was wondering what's behind these convinient helpers. The Laravel string helpers is powered by  Illuminate\Support\Str class. Let's explore what methods it contains and how we can use them. For this example I will be using 'Lorem Ipsm ....' for subject testing. Here's the list of all the methods used, you can jump right to your desired method from here:

  1. after()
  2. ascii()
  3. before()
  4. camel()
  5. contains()
  6. endsWith()
  7. finish()
  8. is()
  9. kebab()
  10. length()
  11. limit()
  12. lower()
  13. words()
  14. plural()
  15. random()
  16. replaceArray()
  17. replaceFirst()
  18. replaceLast()
  19. start()
  20. upper()
  21. title()
  22. singular()
  23. slug()
  24. snake()
  25. startsWith()
  26. studly()
  27. substr()
  28. ucfirst()

1- after($subject, $search) : string

This method chops off all the characters including $search from the start in the $subject and returns the rest of the string. Let's test this method:

Str::after() Method usage
Str::after() Method usage

 

Str::after() Method output
Str::after() Method output

 


2- ascii($value, $language = 'en') : string

 This method returns the ascii equivalent of the $value string in the $language specified.

Laravel string manipulation | ascii() method usage
Laravel string manipulation | ascii() method usage
Laravel string manipulation | ascii() method output
Laravel string manipulation | ascii() method output

 


3- before($subject, $search) : string

This is opposite of after() method. This will only keep the characters before the$search characters:

Laravel string manipulation | before() method usage
Laravel string manipulation | before() method usage
Laravel string manipulation | before() method output
Laravel string manipulation | before() method output

4- camel($value) : string

This method will return a camel-case string. It will remove any spaces and join the words as shown below:

Laravel string manipulation | camel() method usage
Laravel string manipulation | camel() method usage
Laravel string manipulation | camel() method output
Laravel string manipulation | camel() method output

5- contains($haystack, $needles) : bool

This method returns boolean true or false if a string $haystack contains a string / characters $needles. This is a case-sensitive method, means if it has a work 'tomorrow', it will return false for 'Tomorrow' as demonstrated below:

Laravel string manipulation | contains() method usage
Laravel string manipulation | contains() method usage
Laravel string manipulation | contains() method output
Laravel string manipulation | contains() method output

6- endsWith($haystack, $needles) : bool

 This method tests if $haystack ends with $needles which can be a string or an array. Let's check the example below:

Laravel String Mmanipulation | endsWith() method usage
Laravel String Mmanipulation | endsWith() method usage
Laravel String Mmanipulation | endsWith() method output
Laravel String Mmanipulation | endsWith() method output

7- finish($value, $cap) : string

 This method add a padding to the end of the string. In other words, right-pad the string, as shown below:

Laravel String Manipulation | finish() method usage
Laravel String Manipulation | finish() method usage
Laravel String Manipulation | finish() method output
Laravel String Manipulation | finish() method output

8- is($pattern, $value) : bool

 This method tests $value against the $pattern which can be a string or an array of strings and returns true or false if the string contains the pattern. As shown below in the example:

Laravel String Manipulation | is() method usage
Laravel String Manipulation | is() method usage
Laravel String Manipulation | is() method output
Laravel String Manipulation | is() method output

9- kebab($value) : string

 This method turns your string into a kebab-case string.

Laravel String Manipulation | kebab() method usage
Laravel String Manipulation | kebab() method usage
Laravel String Manipulation | kebab() method output
Laravel String Manipulation | kebab() method output

10- length($value, $encoding = null) : int

This method uses mbstring extension's mb_strlen() function for length, for non-ascii (Unicode) strings you have to provide $encoding scheme name such as UTF-8, UTF-16 etc.

Laravel String Manipulation | length() method usage
Laravel String Manipulation | length() method usage
Laravel String Manipulation | length() method output
Laravel String Manipulation | length() method output

11- limit($value, $limit = 100, $end = '...') : string

This method limits the string at a $limit, the default value for limit is 100 characters which is excluding the $end text.

Laravel String Manipulation | limit() method usage
Laravel String Manipulation | limit() method usage
Laravel String Manipulation | limit() method output
Laravel String Manipulation | limit() method output

12- lower($value) : string

 This method converts the string to a lower-case string.

Laravel String Manipulation | lower() method usage
Laravel String Manipulation | lower() method usage
Laravel String Manipulation | lower() method output
Laravel String Manipulation | lower() method output

13- words($value, $words = 100, $end = '...') : string

 This method limits the string on words just like limit() method.

Laravel String Manipulation | words() method usage
Laravel String Manipulation | words() method usage
Laravel String Manipulation | words() method output
Laravel String Manipulation | words() method output

14- plural($value, $count = 2) : string

 This method converts a given word or last word of the string to plural form.

Laravel String Manipulation | plural() method usage
Laravel String Manipulation | plural() method usage
Laravel String Manipulation | plural() method output
Laravel String Manipulation | plural() method output

15- random($length = 16) : string

 This method generates a random string, the default length of the returned string is 16.

Laravel String Manipulation | random() method usage
Laravel String Manipulation | random() method usage
Laravel String Manipulation | random() method output
Laravel String Manipulation | random() method output

16- replaceArray($search, array $replace, $subject)

 This method replaces all the instances of $search in $subject with $replace in a starting order, you will get more clarity with the following example:

Laravel String Manipulation | replaceArray() method usage
Laravel String Manipulation | replaceArray() method usage
Laravel String Manipulation | replaceArray() method output
Laravel String Manipulation | replaceArray() method output

17- replaceFirst($search, $replace, $subject) : string

 This method will only replace the first occurance of $search with $replace in the $subject string as shown in the example below:

Laravel String Manipulation | replaceFirst() method usage
Laravel String Manipulation | replaceFirst() method usage
Laravel String Manipulation | replaceFirst() method output
Laravel String Manipulation | replaceFirst() method output

18- replaceLast($search, $replace, $subject) : string

 Like replaceFirst() this method replaces the last occurance of $search with $replace in the $subject string.

Laravel String Manipulation | replaceLast() method usage
Laravel String Manipulation | replaceLast() method usage
Laravel String Manipulation | replaceLast() method output
Laravel String Manipulation | replaceLast() method output

19- start($value, $prefix) : string

 This method adds $prefix to the starting of the $value. In other words it left-pads the string.

Laravel String Manipulation | start() method usage
Laravel String Manipulation | start() method usage
Laravel String Manipulation | start() method output
Laravel String Manipulation | start() method output

20- upper($value) : string

 This method converts $value to upper-case string. It uses mb_strtoupper().

Laravel String Manipulation | upper() method usage
Laravel String Manipulation | upper() method usage
Laravel String Manipulation | upper() method output
Laravel String Manipulation | upper() method output

21- title($value) : string

 This method converts $value to title-case string.

Laravel String Manipulation | title() method usage
Laravel String Manipulation | title() method usage
Laravel String Manipulation | title() method output
Laravel String Manipulation | title() method output

22- singular($value) : string

 Opposite ofplural() method, it converts plural word $valule into a singular word.

Laravel String Manipulation | singular() method usage
Laravel String Manipulation | singular() method usage
Laravel String Manipulation | singular() method output
Laravel String Manipulation | singular() method output

23- slug($title, $separator = '-', $language = 'en') : string

Turns given string into a slug-case string which can be useful for SEO friendly urls. It will remove any non-printable characters, special characters and convert unicode into ascii characters.

By default, the separator used is '-' but you can specify your own, like I used a '+' sign:

Laravel String Manipulation | slug() method usage
Laravel String Manipulation | slug() method usage
Laravel String Manipulation | usage() method output
Laravel String Manipulation | slug() method output

24- snake($value, $delimiter = '_') : string

This method will convert the string $value into a snake-case string with $delimiter specified, an _ will be used if unspecified.

Laravel String Manipulation | snake() method usage
Laravel String Manipulation | snake() method usage
Laravel String Manipulation | snake() method output
Laravel String Manipulation | snake() method output

25- startsWith($haystack, $needles) : bool

This method tests if a given string $haystack starts with a string or one of strings in the array $needles.

Laravel String Manipulation | startsWith() method usage
Laravel String Manipulation | startsWith() method usage
Laravel String Manipulation | startsWith() method output
Laravel String Manipulation | startsWith() method output

26- studly($value) : string

This method will return a studly-case string of $value.

Laravel String Manipulation | studly() method usage
Laravel String Manipulation | studly() method usage
Laravel String Manipulation | studly() method output
Laravel String Manipulation | studly() method output

27- substr($string, $start, $length = null) : string

This method returns a sub-string of a $string from position $start, $length is optional but you can specify to return the number of characters to be return, it will return the rest of the string if not specified.

This method uses mbstring's mb_substr() function:

Laravel String Manipulation | substr() method usage
Laravel String Manipulation | substr() method usage
Laravel String Manipulation | substr() method output
Laravel String Manipulation | substr() method output

28- ucfirst($string) : string

 I don't know how this method compares to the PHP native ucfirst() function in terms of speed and memory usage but it's there. This method capitalizes the first character / letter of the string given.

Laravel String Manipulation | ucfirst() method usage
Laravel String Manipulation | ucfirst() method usage
Laravel String Manipulation | ucfirst() method output
Laravel String Manipulation | ucfirst() method output