Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace by order [duplicate]

Tags:

replace

php

How can I replace something orderly? for example:

$var = "let's count: ?, ?, ?";

Now I want to replace it by 1, 2, 3 for the result to be "let's count 1, 2, 3". How can I do that? I can't replace by "%s" and use vsprintf() with an array because I wanna respect integers and floats. But I want to do it with an array. For example:

$var = replace( $var, "?", array(1, 2, 3) );

How can I do it? I can't get any idea.

like image 679
Zerquix18 Avatar asked Feb 06 '26 01:02

Zerquix18


1 Answers

How about:

$var = "let's count: ?, ?, ?";
$numbers = array(1,2,3);

foreach( $numbers as $number ) {
  $var = preg_replace("/\?/", $number, $var, 1);
}
like image 53
Cully Avatar answered Feb 07 '26 14:02

Cully