0

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...

Community
  • 1
  • 1
  • possible duplicate of [Str_replace for multiple items](http://stackoverflow.com/questions/7605480/str-replace-for-multiple-items) – Wesley Murch May 11 '12 at 04:54
  • 1
    Just a side note, you might want `trim($input, '{}');` if you only want to remove the left and right sides, it's a little shorter and more to the point. What *is* the data? – Wesley Murch May 11 '12 at 04:59

6 Answers6

2
$str = str_replace(array('}', '{'), '', $str);

str_replace accepts arrays as its first and second argument

zerkms
  • 249,484
  • 69
  • 436
  • 539
2

you can give an array to str replace see

$search = array("}", "{");
$text= str_replace($search, "", $text);

read about it here: str-replace

mbouzahir
  • 1,424
  • 13
  • 16
1
str_replace (array('}', '{'), '', $str);
iiro
  • 3,132
  • 1
  • 19
  • 22
1
$str = str_replace(array('{', '}'), '', $str);
Josh
  • 8,082
  • 5
  • 43
  • 41
DQM
  • 562
  • 3
  • 15
0

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);
EmmanuelG
  • 1,051
  • 9
  • 14
0

$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\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';

// Processes \r\n'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;
?>
Abhishek Jaiswal
  • 2,524
  • 1
  • 21
  • 24