Search and replace multiple values with multiple/different values in PHP5?
You are looking for str_replace()
.
$string = 'blah blarh bleh bleh blarh';
$result = str_replace(
array('blah', 'blarh'),
array('bleh', 'blerh'),
$string
);
// Additional tip:And if you are stuck with an associative array like in your example, you can split it up like that:
$searchReplaceArray = array(
'blah' => 'bleh',
'blarh' => 'blerh'
);
$result = str_replace(
array_keys($searchReplaceArray),
array_values($searchReplaceArray),
$string
);
PHP replace multiple value using str_replace?
From the manual page for str_replace():
For example, "m" is replaced with "Month", then the "h" in "Month" is replaced with "Hours", which comes later in the array of replacements.Caution
Replacement order gotcha
Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.
strtr() doesn't have this issue because it tries all keys of the same length at the same time:
$date = strtr($key, array(
'y' => 'Year',
'm' => 'Month',
'd' => 'Days',
'h' => 'Hours',
'i' => 'Munites', // [sic]
's' => 'Seconds',
));
Str_replace for multiple items
str_replace()
can take an array, so you could do:
$new_str = str_replace(str_split('\\/:*?"<>|'), ' ', $string);
Alternatively you could use preg_replace()
:$new_str = preg_replace('~[\\\\/:*?"<>|]~', ' ', $string);
how to Str_replace for multiple items
You can use strtr()
and an associative array to do this:
<?php
$text = "Text about -- and -+- !";
$replacements = [
"--" => "/",
"-+-" => ":",
];
echo strtr($text, $replacements); // Text about / and : !
To add more replacements, simply keep adding more elements to the $replacements
array. Index is the string to look for, value is the replacement.- Demo
- strtr() reference
replace multiple values in a string
A simple option would be chartr
chartr('1234', '0000', string)
#[1] "0 0 0 0 5 6 7 8 9"
How to replace multiple values in php
This should work for you:
<?php
$string = "test1 test1 test2 test2 test2 test1 test1 test2";
echo $string . "<br />";
echo $string = strtr($string, array("test1" => "test2", "test2" => "test1"));
?>
Output:test1 test1 test2 test2 test2 test1 test1 test2
test2 test2 test1 test1 test1 test2 test2 test1
Checkout this DEMO: http://codepad.org/b0dB95X5
Related Topics
Converting Array and Objects in Array to Pure Array
Find All Second Level Keys in Multi-Dimensional Array in PHP
Php:Capturing the Command Output
Codeigniter $This->Input->Post() Empty While $_Post Is Working Correctly
How to Put Double Quotes Inside a String Within an Ajax JSON Response from PHP
In Magento 1.9.0.0 How to Enable Template Path Hint
Image Upload from iPhone Strips Exif Data
Checking If String Contains "Http://"
File_Get_Contents() Failed to Open Stream:
Unchecked Checkbox Returning Null Value
Phpstorm: How to Add Method Stubs from a Pecl Library That PHPstorm Doesn't Currently Support
How to Assign PHP Array Values to JavaScript Array
How to Convert Arabic Characters to Unicode Using PHP
How to Use Session Variables in Wordpress
How to Wrap Text Using Imagick in PHP So That It Is Drawn as Multiline Text
How to Auto-Resize a Div with CSS While Keeping Aspect Ratio