How to replace multiple characters in a string in PHP
Last Updated on Dec 14, 2022 - Written By Torikul Islam
You can use str_replace()
function to replace multiple characters from a string. It is a PHP built-in function. A string is a sequence of number of characters enclosed within single or double-quotes.
If you want to replace multiple characters at once, you need to call array()
function within str_replace()
function. You can do it in different ways.
Let’s do it.
Basic syntax
str_replace('substring', 'replaced_string', 'original_string');
We need to provide three parameters for both methods.
1. Substring is the first parameter which should be replaced with replaced_string.
2. Replaced_string will replace the substring.
3. Original_string will contain the full string from where you want to replace characters.
In the placement of parameters, you can use PHP variables.
Discussion: Replace multiple characters from a string
Lets explore the task practically. Doing it with some essential example.
Example 1: Replace characters using the str_replace() function
<?php
//input string
$string = '\"\We<lco?m>e/t*o:M-y/Web*site';
//replacing multiple words using array
$content = str_replace(array(':','\\','/','"','<','>','?','-','*'), '', $string);
Or
// defining array (PHP 5.4 +)
$content = str_replace([':','\\','/','"','<','>','?','-','*'], '', $string);
//showing result
echo $content;
?>
Output:
Original string: \"\We<lco?m>e/t*o:M-y/Web*site
Modified string: WelcometoMyWebsite
The str_replace() function is using replace multiple characters in a string. This function takes three parameters.
The first parameter includes an array of characters which need to replace. The array is constructed by first defining a sequence of characters to replace. Just include the character you want to replace.
The seconds parameter will eliminate the characters with empty value. And the third parameter consist of input string the string on which this operation is performed.
To eliminate complexity, you can use str_split() function.
Example 1: Replace characters using the str_split() function
<?php
//input string
$string = '\"\We<lco?m>e/t*o:M-y/Web*site';
//replacing multiple characters
$content = str_replace(str_split(':\\/"<>?*|+-'), '', $string);
//showing result
echo $content;
?>
Output:
Original string: \"\We<lco?m>e/t*o:M-y/Web*site
Modified string: WelcometoMyWebsite
In this example, the characters to be replaced are ‘:\\/"<>?*|+-‘ and the character replacing these all characters is the empty value.
Alternatively, you could use preg_replace()
Example 3: Replace characters using the preg_replace() function
<?php
//input string
$string = '\"\We<lco?m>e/t*o:M-y/Web*site';
//replacing multiple characters
$content = preg_replace('~[\\\\/:"<>?*|+-]~', '', $string);
//showing result
echo $content;
?>
Output:
Original string: \"\We<lco?m>e/t*o:M-y/Web*site
Modified string: WelcometoMyWebsite
In this example, the characters to be replaced are ‘\/:"<>?*|+-‘ and the character replacing these all characters is the empty value.