I want replace php codes in string and running fro server
$x = 1;
$string = "OK:{first}Yes{second}No";
I want replace if($x == 1){ to {first} and } else { to {second}
After run and echo $string , I want result in html :
OK:Yes
How did you come up with this approach? Why not simply:
$string = $x == 1 ? 'OK:Yes' : 'OK:No';
That would get you the string you want based on the value of $x.
Also if you literally mean that you want to do a search-n-replace on the PHP code itself (as @OllyTenerife assumes), then are you going to write it into a file to be executed later, or use eval() or something? Doesn't sound like the right track there... In which case, remodel your code.
eval() to then evaluate the code you've stored in that string. Using eval() is a very bad idea for a templating engine. You'd be better off just using e.g. {result} in your HTML, and then doing a search-replace over your HTML, where you replace {result} with the desired value. Use the code above, and then run str_replace('{result}', $string, $HTMLcode);, where $HTMLcode is your template file.preg_replace() to accomplish your job -- and you will have to craft a bunch of regex patterns, assuming you have more than one token you intend to replace. In any case, trying to search-n-replace PHP code into a string for evaluation is not a good idea.