0

I have the following:

$string="rgb(Unknown characters) some text rgb(Unknown characters)";

I want to replace the the text Unknown characters with some replacement. I have tried this:

echo str_replace("Unknown characters","some replace characters",$string);

This works for an exact text match but I don't know what is inside the parentheses.

I would like the output to be

rgb(some replace characters) some text rgb(some replace characters)

Please help me.

twoleggedhorse
  • 4,938
  • 4
  • 23
  • 38

1 Answers1

2

Use preg_replace with a regex:

$string="rgb(Unkown characters) some text rgb(Unkown characters)";
echo preg_replace("/\([^)]+\)/","(some replace characters)",$string);

Output:

rgb(some replace characters) some text rgb(some replace characters)

Regex:

/         : regex delimiter
  \(      : open parenthesis
  [^)]+   : 1 or more any character that is not close parenthesis
  \)      : close parenthesis
/         : regex delimiter
Toto
  • 89,455
  • 62
  • 89
  • 125