How to get the substring before and after a character in Laravel
String manipulation is the most widely used task in web development, and Laravel provides a wide range of functions and helper methods to make it easier. Here, we'll explore some of the most widely used methods to get the substring before and after a specific character in Laravel. Once we have obtained the substrings, we can perform various operations on them. For example, we might want to store them in variables, pass them as arguments to functions, or manipulate them further based on our application's requirements.
The strtok() function
This function is helpful for breaking a string into smaller parts based on a delimiter. It returns the part of the string before the first occurrence of the delimiter and updates the internal pointer to the next part.
Parameters and return value
The strtok() function takes a string and a delimiter respectively as its parameters. If we pass an empty delimiter, it will return the exact string. The same return value will be received as an output if the delimiter is not found inside the given string.
<?php$string = 'Hello-Educative';$delimiter = '-';$before = strtok($string, $delimiter); // Hello$after = strtok($delimiter); // Worldecho $before;echo "\n"."\n";echo $after;?>
Note: We need to call
strtok()twice to get both the before and after substrings.
The explode() function
In Laravel, we can use this function to split a string into an array based on a delimiter character. The following is an example of extracting substrings before and after a specific character in a given string.
cGhwOgogIHByZXNldDogbGFyYXZlbAogIHZlcnNpb246IDgKICBkaXNhYmxlZDoKICAgIC0gbm9fdW51c2VkX2ltcG9ydHMKICBmaW5kZXI6CiAgICBub3QtbmFtZToKICAgICAgLSBpbmRleC5waHAKICAgICAgLSBzZXJ2ZXIucGhwCmpzOgogIGZpbmRlcjoKICAgIG5vdC1uYW1lOgogICAgICAtIHdlYnBhY2subWl4LmpzCmNzczogdHJ1ZQo=
In this example, the original string is "Hello, Educative" and the delimiter is a comma (,). The explode() function takes the delimiter and the string as parameters and returns an array of substrings. By accessing the first element of the array ([0]), we can get the substring before the delimiter, which in this case is the "Hello" keyword. Similarly, accessing the second element of the array ([1]) gives us the substring after the delimiter, which is "Educative."
Parameters and return value
The explode() function takes a delimiter and a string respectively as its parameters. If the delimiter is an empty string, the explode() function throws an error. If the delimiter contains a value that is not found in the string, then an empty string will be returned. If the delimiter value is at the start of the string, then $substringBefore will return an empty string. If the delimiter value is at the end of the string, then $substringAfter will return an empty string.
The before and after functions
These string-helper methods return everything before and after the given value in a string. The following is a demonstration of how we can use them inside our Laravel application.
Parameters and return value
Both before and after take two string inputs: the main string and the value (substring) that is searched within the main string. The entire string will be returned if the value doesn't exist within the $string variable. The same is the case if we pass an empty value, and it will return the same string passed to the $string variable. The same response will be observed for both the before and after functions. If the value passed is at the start of a string, the before function will return an empty string. If the value passed is at the end of a string, the after function will also return an empty string.
cGhwOgogIHByZXNldDogbGFyYXZlbAogIHZlcnNpb246IDgKICBkaXNhYmxlZDoKICAgIC0gbm9fdW51c2VkX2ltcG9ydHMKICBmaW5kZXI6CiAgICBub3QtbmFtZToKICAgICAgLSBpbmRleC5waHAKICAgICAgLSBzZXJ2ZXIucGhwCmpzOgogIGZpbmRlcjoKICAgIG5vdC1uYW1lOgogICAgICAtIHdlYnBhY2subWl4LmpzCmNzczogdHJ1ZQo=
The beforeLast and afterLast functions
The beforeLast and afterLast methods return everything before or after the last occurrence of a given value in a string, respectively.
Parameters and return value
Both beforeLast and afterLast take two string inputs: the main string and the value (substring) that is searched within the main string. The entire string will be returned if the value doesn't exist within the string. The same is the case if we pass an empty value, and it will return the same string. The same response will be observed for both the beforeLast and afterLast functions. If the value passed is at the start of a string, the beforeLast function will return an empty string. If the value passed is at the end of a string, the afterLast function will also return an empty string.
cGhwOgogIHByZXNldDogbGFyYXZlbAogIHZlcnNpb246IDgKICBkaXNhYmxlZDoKICAgIC0gbm9fdW51c2VkX2ltcG9ydHMKICBmaW5kZXI6CiAgICBub3QtbmFtZToKICAgICAgLSBpbmRleC5waHAKICAgICAgLSBzZXJ2ZXIucGhwCmpzOgogIGZpbmRlcjoKICAgIG5vdC1uYW1lOgogICAgICAtIHdlYnBhY2subWl4LmpzCmNzczogdHJ1ZQo=
Free Resources