I have a string e.g.
$str = "name is: ?, role is: ?";
and I have an array, e.g.
$array = array('John','Carpenter');
I want to replace each ? in the string with the corresponding item in the array, i.e. the first ? gets replaced with John and the second ? gets replaced with Carpenter.
The number of values in the array will always be the same as the number of ? in the string.
I have come up with the following code to do this:
for($i=0;$i<count($array);$i++) {
$str = preg_replace('/\?/',$array[$i],$str,1);
}
My question is, is this the most efficient way of doing what I want to do? If you know of a method, or more efficient way of doing this, please could you post an answer?
Many thanks.
$str = vsprintf(str_replace('?','%s',$str), $array);