Is there any possibility to use str_replace
for multiple value replacement in a single line. For example i want to replace ' '
with '-'
and '&'
with ''
?

- 19,892
- 8
- 62
- 70

- 651
- 2
- 7
- 15
-
1Does this answer your question? [How to replace multiple items from a text string in PHP?](https://stackoverflow.com/questions/9393885/how-to-replace-multiple-items-from-a-text-string-in-php) – ggorlen Jan 07 '20 at 19:33
10 Answers
str_replace()
accepts arrays as arguments.
For example:
$subject = 'milk is white and contains sugar';
str_replace(array('sugar', 'milk'), array('sweet', 'white'), $subject);
In fact, the third argument can also be an array, so you can make multiple replacements in multiple values with a single str_replace()
call.
For example:
$subject = array('milk contains sugar', 'sugar is white', 'sweet as sugar');
str_replace(array('sugar', 'milk'), array('sweet', 'white'), $subject);
As others have noted, this is clearly stated in the manual:
search The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
replace The replacement value that replaces found search values. An array may be used to designate multiple replacements.
subject The string or array being searched and replaced on, otherwise known as the haystack.

- 19,892
- 8
- 62
- 70
-
Why is this answer not at the top? I almost wrote a `foreach` statement reading the above answers. – return_false Mar 13 '21 at 23:00
-
You might have selected to sort answers by date rather than by rating. Often that would give the most current and relevant answer, but it would also push down the highly rated or accepted answers. – Boaz Mar 13 '21 at 23:03
-
$name = 'abcd';
I want to replace 'a' with '$' and 'b' with '!', so I need to write like this:
$str = ['a','b'];
$rplc =['$','!'];
echo str_replace($str,$rplc,$name);
output : $!cd

- 191
- 1
- 5
-
This answer was clearly not tested and is riddled with script-killing typos. How did this answer receive any UVs? – mickmackusa Nov 16 '20 at 12:11
Take note of the order of the arrays as it corresponds to the order.
Like for below, A is replaced with B, B with C and so on.. so there you go.
// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);
// Outputs: apearpearle pear
// For the same reason mentioned above
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
echo $output;
?>
source: PHP str_replace

- 452
- 4
- 17
You can create a symbols array consisting of various symbols that you want to replace and the array of replacements you want and finally call str_replace($symbols,$your_replacements_array_variable,$your_data_variable). Here is a snippet.
$symbols=array(' ','!','"','#','$','%','&','\'','(',')','*','+',',','-','.','/',':',';','<','>','=','?','@','[',']','\\','^','_','{','}','|','~','`');
$replacement=array('');// you can enter more replacements.
$fTitle=str_replace($symbols,$replacement,$fTitle);

- 256
- 2
- 7
Simply you can use str_replace this way:
$string = "This code is neat & clear.";
str_replace([' ', '&'], ['-', ''], $string);
// Output will be "This-code-is-neat--clear."

- 197
- 1
- 7
This is how I did it to replace ' to '' so it doesn't break in SQL queries and " " with "" because people kept adding a space at the end of their emails:
$check = str_replace("'","''",$_POST['1']);
$checkbox1 = str_replace(" ","",$check);
For yours you could do this:
$check = str_replace("&","",$_POST['1']);
$checkbox1 = str_replace(" ","-",$check);

- 31
- 2
You can pass all those values which you did not want / or want to replace in an array and apply str_replace in the following form
$username='String here with anything';
$remove[] = "'";
$remove[] = '"';
$remove[] = "select";
$username = str_replace( $remove, "", $username );

- 463
- 5
- 5
-
you can make an array like this as well of the values you dont want /replace in string $replace_str = array('"', "'", ","); – Hassan Qasim Oct 09 '21 at 08:29
When you want to replace different text strings with the same string (or no text - blank), you can use an array for the search object and a single string for the replacement.
$subject = 'milk XCVBN is white ZXCVB & contains QWERT sugar';
str_replace(array('QWERT ', 'XCVBN ', 'ZXCVB '), '', $subject);
returns:
milk is white & contains sugar

- 416
- 1
- 6
- 11
Yes with the help of str_replace function we can do multiple value replacement in a single line without array.Here is my code
echo str_replace(" ","-",str_replace("&","","I like Tea&Coffee"));

- 35
- 1
- 7