Replace first occurrence of a substring in a string in PHP
This article demonstrates how to replace the first occurrence of a substring in a string in PHP.
1. Using implode()/explode() function
You can replace the first occurrence of a substring using the implode() and explode() functions. The idea is to divide the string into two strings: one that contains all characters before the searched string, and one that contains all characters after the searched string. Finally, join both strings and return them.
The following code uses the limit parameter of the explode() to restrict the replacement to the first occurrence only. You can skip the limit parameter to replace all occurrences of the substring.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php function left_replace($text, $find, $replace) { return implode($replace, explode($find, $text, 2)); } $text = 'prefix_str_suffix'; $find = 'fix'; $replace = '***'; $str = left_replace($text, $find, $replace); echo $str; /* Output: pre***_str_suffix */ ?> |
2. Using substr_replace() function
You can use the substr_replace() function to replace text within a portion of a string. It takes four parameters, in that order: the input string, the replacement string, the offset position, and optionally the length. The following solution replaces the first occurrence of a searched string in the given string using the str_replace() function. It uses strpos() function to determine the position of the first occurrence of the substring in a string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php function left_replace($text, $find, $replace) { $pos = strpos($text, $find); if ($pos !== false) { $text = substr_replace($text, $replace, $pos, strlen($find)); } return $text; } $text = 'prefix_str_suffix'; $find = 'fix'; $replace = '***'; $str = left_replace($text, $find, $replace); echo $str; /* Output: pre***_str_suffix */ ?> |
As a bonus, here’s the PHP equivalent of Java’s removeCharAt() function that can be implemented with substr_replace() function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php function removeCharAt($str, $pos) { return substr_replace($str, "", $pos, 1); } $text = 'Hello, World'; $pos = strpos($text, ","); // find position of comma $str = removeCharAt($text, $pos); // remove comma echo $str; /* Output: Hello World */ ?> |
3. Using preg_replace() function
Alternatively, you can replace the first occurrence of a substring in a string using the preg_replace() function. The preg_replace() function uses a regular expression for search and replace. The following code replaces the first occurrence of a substring within a string using preg_replace(). It uses limit = 1 (the fourth parameter) to restrict the maximum possible replacements for the specified pattern to the first occurrence. You can skip the limit parameter to replace all occurrences of the substring within the string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php function left_replace($text, $find, $replace) { return preg_replace("/$find/", $replace, $text, 1); } $text = 'prefix_str_suffix'; $find = 'fix'; $replace = '***'; $str = left_replace($text, $find, $replace); echo $str; /* Output: pre***_str_suffix */ ?> |
That’s all about replacing the first occurrence of a substring 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 :)