I want to replace placeholder into a given string by using PHP (no JavaScript).
For that I have a list of possible placeholders and the new string for each:
$placeholder=array( 'firstplaceholder'=>'new text for ph1',
'2ndplaceholder'=>'new text for ph2',
'3placeholder'=>'new text for ph3',
'4placeholder'=>'new text for ph4'
...
And the string with the placeholders
$string='This is my text, here I want to use {2ndplaceholder}";
Normally I will do it in this way:
foreach($placeholder as $k=>$e)
{
if(strpos ( $string , $k){ $string=str_replace('{'.$k.'}',$e,$string);
}
Now I think about runtime. If I have a big list of placeholders it makes more sense to check if I have placeholders in the string and replace them instead loop each placeholder if I only need few of them.
How can I make it or how can I create a array from the string which has all placeholders from the string, to loop only them?
strtris an alternative that does the same job.