Reputation: 701
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 ''
?
Upvotes: 49
Views: 120446
Reputation: 486
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
Upvotes: 0
Reputation: 267
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."
Upvotes: 1
Reputation: 256
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);
Upvotes: 3
Reputation: 483
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 );
Upvotes: 0
Reputation: 186
$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
Upvotes: 8
Reputation: 31
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);
Upvotes: 0
Reputation: 35
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"));
Upvotes: -2
Reputation: 20230
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.
Upvotes: 101
Reputation: 452
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
Upvotes: 3