Replace String in PHP (4 Easy Methods)
Replacing string in PHP is an easy task to perform but is most useful in PHP web development. When we work with user inputs, APIs, and processing data it comes in handy. In this article, we’ll explore different methods to replace a string in PHP.
As you know, PHP is a server-side language and we mostly use it to manipulate and process different types of data which includes strings. Before going to see the main part, let’s understand the basic structure of the string.
What is a String in PHP?
In PHP, a string is simply a sequence of characters enclosed in double or single quotes. A string can be a single character, a word, a sentence, or even a paragraph. Strings are a crucial part of any programming language not just in PHP. Because we use strings to store text-based data such as usernames, passwords, email addresses, or text messages.
Now, you have a basic understanding of a string, let’s see how to replace a string.
Tip: If you’re interested in learning more about strings then check out this tutorial about PHP strings.
How to Replace a String in PHP
PHP gives us several ways to replace a string with another string or even a single character inside a string. The most easiest and common methods to replace strings in PHP are by using the following four functions given below:
- str_replace() function
- str_ireplace() function
- substr_replace() function
- strtr() function
Let’s see explore these PHP functions one by one.
1. Use PHP str_replace() Function
PHP str_replace() is a commonly used function to replace strings in PHP. This function replaces all the words or characters of a given string with the desired words or characters. The function consists of three parameters as shown below in the code example.
<?php str_replace($search, $replace, $orginal_string); ?>
Parameters of PHP str_replace() function
- $search (first parameter) – This parameter contains the text in the form of a string that you want to replace. The text can be stored in a string.
- $replace (second parameter) – This parameter contains text in the form of a string that you want to replace with.
- $original_string (third parameter) – The third parameter is the original string in which we want to make changes.
- $count (optional) – this parameter checks the number of changes and is not crucial to use.
In simple words, the str_replace() function finds the given word or character from the given string $string. And replaces that word with the given words, phrase, or character stored in the $replace variable.
How to use str_replace() function
Let’s see the code example on how to use the str_replace() function to replace a string in PHP.
<?php $string = "PHP Hello World"; $rs = str_replace("Hello", "Hi", $string); echo $rs; ?>
In the above code example, we replace the word “Hello” with “Hi” using the PHP string replace function. First, we created a variable called $string and stored “PHP Hello World” as a string inside it. Then, we declared a new variable and named it $rs, and used the str_replace() function. And then we used an echo statement to print the output on the screen which is shown below.
Pros and Cons of str_string() Function
Here are some pros and cons of using the PHP str_string() function to replace a string.
- Simple and easy to use. Good for string replacements.
- It’s fast and can replace a large number of strings efficiently.
- str_string() function is used for both case-sensitive and case-insensitive strings.
- You can replace multiple strings using PHP str_string() function at the same time.
- You can replace any type of string including special characters, numbers, and alphanumeric characters.
- This function is limited and replaces exact string matches, not regular expressions or matches.
- The str_replace() function also provides limited control for string replacements.
However, there is always an alternative. So, let’s see the second method to replace a string.
2. Use str_ireplace() Function to Replace String
The str_ireplace() function is another built-in PHP function that is used to replace a string with another string. Basically, it’s quite similar to the previous function we used. The syntax and procedure to use the str_ireplace() function are the same as the str_replace() function.
Difference Between str_replace() and str_ireplace() functions
The main difference between the str_ireplace() and str_replace() functions is the case sensitivity. Let’s understand it with a code example.
<?php $string = "PHP Hello World"; // it's the case-sensitive function $first_result = str_replace("hello", "Hi", $string); // it's the case-insensitive function $second_result = str_ireplace("hello", "Hi", $string); echo $first_result . "<br>"; echo $second_result; ?>
Take a close look at the above code, and let’s find out what’s different. In the above code, I used “h” small instead of using capital “H” inside the first parameter of both functions which is “hello”. Now, if you see the output of the above code in the image below the second string is replaced but the first is the same as it is.
Why the first function didn’t change the value? Because the first function str_replace() to replace a string is case-sensitive, and it accepts the exact match. For this function “hello” and “Hello” are not the same. While the second function str_ireplace() is case-insensitive and works perfectly.
Both functions have their own pros and cons. And works perfectly to replace a string in PHP. Let’s see.
Pros and Cons of PHP str_ireplace() function
- Easy to use and similar to the str_replace() function
- Best for replacing a string content with case-insensitive
- Can be used to replace multiple strings in PHP
- Its performance is slower than the str_replace() function because it performs additional checks to check the case sensitivity.
3. Use PHP substr_replace() Function
The substr_replace() is another PHP function that we can use to replace a string in PHP. It differs from the above two functions because it replaces a portion of a string with another string. And it takes four parameters.
Here’s the syntax to use PHP substr_replace() function to replace a string.
<?php substr_replace($string, $replacement, start, length); ?>
- $string (first parameter): It’s the original string in which we want to make changes.
- $replacement (second parameter): It’s the text string to replace with.
- start (third parameter): The start parameter specifies the position from where we want to replace the string. Remember the counting starts from 0.
- length: The length parameter specifies the length of the string, it’s the portion of the string that will change.
How to use the substr_replace() Function
Let’s see the code example of the substr_replace() function to replace a string in PHP.
<?php $string = "PHP is the best programming language"; $rs = substr_replace($string, "great", 11, 4); echo $rs; ?>
4. PHP strtr() Function to Replace String Characters
PHP strtr() function is used to replace a set of characters in a string with another set of characters. It is best to use if you perform replacements to characters of a string. Whether you want to replace multiple characters in a string or a single character you can use strtr() function.
The PHP strtr() function takes three parameters as shown in the syntax below:
<?php $rs = strtr($string, from, to); ?>
- $string (first parameter): It’s the string in which we want to make changes.
- From (second parameter): It’s a single character or characters that we want to replace.
- To (third parameter): This is the character that we want to replace with.
How to use strtr() function in PHP
Let’s see a code example to know how to use strtr() function in PHP.
<?php $string = "Its pen"; $rs = strtr($string, "p", "t"); echo $rs; ?>
The above PHP code will replace the character “p” of the string with “t”. The output in your browser will look like this below.
So, this is how you can replace a string in PHP. I hope this helped. If you have any questions related to this guide then leave a comment below.