0

I have a string like, "[one]sdasf[two]sad[three]"

So, I want to replace [one] with "replaced_one", [two] with "replaced_two". and I need those replaced values as separate strings.

$rep_one = "replaced_one";
$rep_two = "replaced_two";
Sam
  • 2,856
  • 3
  • 18
  • 29
Mann
  • 576
  • 1
  • 9
  • 33
  • 2
    Ok great. Your requirements are clear. Now what you have tried so far to overcome your problem?Please add your effort.Otherwise it seems like you are asking us to do code for you – Alive to die - Anant Sep 07 '18 at 12:31

1 Answers1

-1

This is a simple PHP function str_replace() (For more info go to PHP Manual)

The function is expecting three parameters:

str_replace(
  # Search - string section to be replaced
  # Replace - the content which will replace the `Search`
  # Subject - The string you want to alter
)

So in your case you can simply do this:

$str = "[one]sdasf[two]sad[three]";
$str = str_replace('[one]', 'replace_one', $str);
$str = str_replace('[two]', 'replace_two', $str);

echo $str; // replace_onesdasfreplace_twosad[three]
Sam
  • 2,856
  • 3
  • 18
  • 29