Remove part of a string that follows a substring in PHP
This article demonstrates how to remove the part of a string that follows a substring (including the substring) in PHP.
1. Using substr() function
The idea is to find the index of the first occurrence of the substring in the string using the strpos() function. Then extract all characters before that index using the substr() function. This works since removing everything after a substring (including the substring) essentially results in the same string as extracting everything before it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $text = 'The quick brown fox jumps over the lazy dog'; $substring = 'jumps'; $len = strpos($text, $substring); $result = substr($text, 0, $len); echo "\"$result\""; /* Output: "The quick brown fox " */ ?> |
PHP 5.3 eliminates the need for calling the strpos() function by introducing a third parameter to the strstr() function. It is a boolean parameter that, if true, makes substr() return the part of the string before the substring’s first occurrence.
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $text = 'The quick brown fox jumps over the lazy dog'; $substring = 'jumps'; $result = strstr($text, $substring, true); echo "\"$result\""; /* Output: "The quick brown fox " */ ?> |
2. Using explode() function
Another option is to split the string with explode() using the substring as the delimiter. Since the explode() function returns an array of strings, the first item is the answer.
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $text = 'The quick brown fox jumps over the lazy dog'; $substring = 'jumps'; $result = explode($substring, $text)[0]; echo "\"$result\""; /* Output: "The quick brown fox " */ ?> |
3. Using preg_replace() function
You can also use a regular expression to remove the part of a string that follows the given substring. This can be implemented as follows in PHP, using the preg_replace() function for regular expression matching. Here, .* matches zero or more occurrences of any character, and $ matches the end of the string.
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $text = 'The quick brown fox jumps over the lazy dog'; $substring = 'jumps'; $result = preg_replace("/{$substring}(.*)$/", '', $text); echo "\"$result\""; /* Output: "The quick brown fox " */ ?> |
4. Using substr_replace() function
Finally, if you need to remove the part of a string after the specified position, you can use the substr_replace() function. It replaces the input string, delimited by the offset and length parameters, with the replacement string. If the replacement string is an empty string ('') and the length parameter is omitted, then all characters starting from the offset position will be removed.
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $text = 'The quick brown fox over the lazy dog'; $offset = 19; $result = substr_replace($text, '', $offset); echo $result; /* Output: The quick brown fox */ ?> |
5. Using mb_strimwidth() function
Another option to remove the whole portion of a string after the specified position is using the mb_strimwidth() function. It works by truncating the string to the specified width.
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $text = 'The quick brown fox over the lazy dog'; $offset = 19; $result = mb_strimwidth($text, 0, $offset); echo $result; /* Output: The quick brown fox */ ?> |
That’s all about removing the part of a string that follows a substring 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 :)