Extract everything after a certain character in a string in PHP
This article demonstrates how to extract everything after a certain character in a string in PHP.
1. Using substr() function
You can use the substr() function to extract a substring from a string. The two-arg substr() takes an input string and an offset indicating the starting position, and returns a substring starting from the offset until the end of the string. The idea is to find the position of the first occurrence of a given character within the string with strpos(), and then extract everything that follows it using substr().
|
1 2 3 4 5 6 7 8 |
<?php $string = "Ignorance is bliss-You're better-off not knowing"; $token = "-"; $result = substr($string, $index + strlen($token)); echo $result; // You're better-off not knowing ?> |
Before extracting the substring, you might want to check the presence of that character in your string. Otherwise, the solution would return the input string with the first character skipped.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $string = "Ignorance is bliss-You're better-off not knowing"; $token = "-"; $result = ""; $index = strpos($string, $token); if ($index !== false) { $result = substr($string, $index + strlen($token)); } echo $result; // You're better-off not knowing ?> |
Take note of the !== operator. You must not use the != or ! operator here, since strpos() can return a falsey value even on success (index 0 is returned when the search string is located at the very start of the string). The extraction is done using the first occurrence of the character within the string. If you want to extract the substring after the last occurrence of a character, consider using the strrpos() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $string = "Ignorance is bliss-You're better-off not knowing"; $token = "-"; $result = ""; $index = strrpos($string, $token); if ($index !== false) { $result = substr($string, $index + strlen($token)); } echo $result; // off not knowing ?> |
2. Using explode() function
Alternatively, you can split the string into two parts using the given character as a delimiter. The first string contains all characters before the delimiter, and the second string contains all characters after the delimiter. This can be done using the explode() function with the limit parameter set to 2, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $string = "Ignorance is bliss-You're better-off not knowing"; $token = "-"; $result = ""; $index = strpos($string, $token); if ($index !== false) { $result = explode($token, $string, 2)[1]; } echo $result; // You're better-off not knowing ?> |
In the case of multiple occurrences of the character within the string, you can extract the substring after the character’s last occurrence by skipping the limit parameter, and returning its last element.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $string = "Ignorance is bliss-You're better-off not knowing"; $token = "-"; $result = ""; $index = strpos($string, $token); if ($index !== false) { $result = @end(explode($token, $string)); } echo $result; // off not knowing ?> |
3. Using strtok() function
Finally, successive calls to the strtok() function will split a string into substrings according to the specified delimiter. The idea is to initially invoke the strtok() function using the given character as a delimiter, which returns the part of the string before the specified delimiter. Then, to obtain the remainder of the input string, make a subsequent call to strtok() with an empty string as the delimiter.
|
1 2 3 4 5 6 7 8 9 |
<?php $string = "Ignorance is bliss-You're better-off not knowing"; $token = "-"; strtok($string, $token); $result = strtok(""); echo $result; // You're better-off not knowing ?> |
It should be noted that the above solution will not work if the delimiter is located at the beginning of the string. However, it can be used to extract the substring after the last occurrence of the character by making successive calls to strtok() with the specified character as delimiter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $string = "Ignorance is bliss-You're better-off not knowing"; $token = "-"; $tok = strtok($string, $token); while ($tok !== false) { $result = $tok; $tok = strtok($token); } echo $result; // off not knowing ?> |
However, strtok() will fail if the delimiter is a substring of the string with a length of two or more. It will tokenize the string when any one of the characters in the delimiter is found.
That’s all about extracting everything after a certain character in a string in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)