I need a simple and fast solution to replace nth occurrence (placeholder) in a string.
For example, nth question mark in sql query should be replaced with a provided value.
$subject = "SELECT uid FROM users WHERE uid = ? or username = ?";
So, i need function like str_replace_nth($seach, $replace, $subject, $nth) and for second question mark it should be called as str_replace_nth("?", $username, $subject, 2);
Any ideas?
P.S. Please, don't suggest me to use PDO, because I'm working on FDO (Facebook Data Object) a library with an interface similar to PDO, but for FQL.
Important notice! I've figured out that this approach is bad because after first replacement the query is modified and indexes are lost. (Bad approaches come when you're programming late at night :() So, as @GolezTrol mention in comment, it's better to replace all at once.
$parameterif you only have question marks? An index? If so, you could save up the values and only process them (in the right order) just before executing the query. That also allows the user to set a default value for a parameter and overwrite it later but before executing. But actually, I like the named parameters solution as proposed by @RaphaëlMalié better.