let's say I have a string like this:
$str = "{aaa,aa,a,aaaaaaaa,aaaaaaa,aaaaaaa}";
I want to remove both {
& }
using str_replace
only once.. Possible?
I've tried
$str = str_replace ('}', '{', '', $str);
$str = str_replace ('}'&'{', '', $str);
$str = str_replace ('}'||'{', '', $str);
$str = str_replace ('}''{', '', $str);
and none works...
$str = str_replace(array('}', '{'), '', $str);
str_replace
accepts arrays as its first and second argument
What you want to do is use the preg_replace function instead which will replace more than one item at a time using regex. What you want to do can be accomplished with:
$str = preg_replace('/({|})/', '', $str);
str_replace (array('}', '{'), '', $str);
$str = str_replace(array('{', '}'), '', $str);
you can give an array to str replace see
$search = array("}", "{");
$text= str_replace($search, "", $text);
read about it here: str-replace
$search = array("}", "{"); $text= str_replace($search, "", $text);
yuo can read more :- http://php.net/manual/en/function.str-replace.php
example
<?php
// Order of replacement
$str = "Line 1
Line 2Line 3
Line 4
";
$order = array("
", "
", "");
$replace = '<br />';
// Processes
's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);
// 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;
?>