Tinkerwell 3 is available now! 🎉 Autocompletion, magic-comments, and more! Get your license now

Go back to Blog

str_replace explained (with examples)

Sebastian Schlein

No one on earth can remember the order of parameters for PHP functions like str_replace. You need this function often but which parameter comes first?

In this post, we go through the parameters of str_replace in PHP, tell you which alternatives there are and how you usually use them with frameworks like Laravel.

If you are a visual learner, skip this post and watch the following gif where we use str_replace within Tinkerwell. Tinkerwell is a tinker tool for PHP developers and allows you to run and evaluate PHP code. It ships with a Laravel app, so that you can run the code within a plain Laravel application but also comes with drivers for bootstrapping most other frameworks, CMS platforms and ecommerce kits.

str_replace parameters

str_replace needs at least three parameters:

  • The term, that you'd like to replace ($find)
  • The new value for each finding ($replacement)
  • The string (or array) to be searched ($string)
$find = 'str_replace';
$replacement = 'preg_match';
$string = 'Nobody remembers str_replace parameters so developers google str_replace all the time.';

$result = str_replace($find, $replacement, $string);

This results in the result string Nobody remembers preg_match parameters so preg_match is googled a lot.. As you can see, both occurences of the string in $find are replaced. It's important to understand that the initial string in $string hasn't changed and in cases where you want to change the string, it's required to assing it to the result of the function.

$string = str_replace($find, $replacement, $string);

Optional parameters

str_replace in PHP has an optional fourth parameter, the $count variable that returns the numer of replacements during that opration.

$result = str_replace($find, $replacement, $string, $count);

After running this on our intial input strings, the $count variable has the value 2.

Case sensitivity

The str_replace function in PHP is is case sensitive – in cases where you can't control the input strings, it can be useful to have a case-insensitive search by using str_ireplace.

str_replace('guys', 'team', 'Hi Guys'); // doesn't work
str_ireplace('guys', 'team', 'Hi Guys'); // replaces `Guys` with `team`

Replacing multiple values at once

You can use PHP and str_replace to replace multiple strings at once. For this, str_replace accepts arrays as input for the intial $find and $replacement parameters.

$find = ['Nobody', 'remembers'];
$replacement = ['Everybody', 'googles'];
$string = 'Nobody remembers str_replace parameters.';

$result = str_replace($find, $replacement, $string);

The result of this replacement operating is Everybody googles str_replace parameters.. The functionen uses the first replacement for the first value and the second replacement for the seconds value.

When replacing multiple strings at the same time, every value is matched against a replacement. If no replacement for a value is found, it's replaced with an empty string – it's deleted from the string.

Replacing strings in Laravel with the str()helper

Laravel offers convenience helpers for string replacements, so if you are working on a Laravel project, make sure to use the helpers instead of str_replaceand str_ireplace directly.

str('Tinkerwell 2.x is awesome')->replace('2.x', '3.x');

$string = 'The quick brown fox jumps over the lazy dog.';

str($string)->replaceFirst('the', 'a');
=>  'a quick brown fox jumps over the lazy dog.'

str($string)->replaceLast('the', 'a');
=>  'The quick brown fox jumps over a lazy dog.'

Its time to Tinkerwell

The must-have tinker tool for every PHP and Laravel developer. This magical desktop app may not be able to fly. But when it comes to PHP code, it sure knows how to run.

Learn more