Replace repeating character with array elements in PHP

Posted by Will Croft on Stack Overflow See other posts from Stack Overflow or by Will Croft
Published on 2010-05-17T13:44:58Z Indexed on 2010/05/17 13:50 UTC
Read the original article Hit count: 264

Filed under:
|

I hope this is blindingly obvious: I'm looking for the fastest way to replace a repeating element in a string with the elements in a given array, e.g. for SQL queries and parameter replacement.

$query  = "SELECT * FROM a WHERE b = ? AND c = ?";
$params = array('bee', 'see');

Here I would like to replace the instances of ? with the corresponding ordered array elements, as so:

SELECT * FROM a WHERE b = 'bee' and c = 'see'

I see that this might be done using preg_replace_callback, but is this the fastest way or am I missing something obvious?

© Stack Overflow or respective owner

Related posts about php

Related posts about php5