proggR Posted May 24, 2011 Share Posted May 24, 2011 I know you can use str_replace to replace a value with another value, or an array of values with an array of other values. What I'd like to do is replace each instance of a single value with an array of values which get iterated through for each instance found. Think prepared statements that you don't have to call setString or setInt on. Here's a made up example. $db->query = 'SELECT * FROM users WHERE user_name = ? AND user_role = ?; $db->params = array('hopelessX','admin'); $db->executeQuery(); //this contains a call to a private function that does the replace I have everything else working except this. It works if I specify an array of values to replace and do something like below, but I was hoping to not have to support that method of replace if I didn't need to. $db->query = 'SELECT * FROM users WHERE user_name = :name AND user_role = :role; $db->replace = array(':name',':role'); $db->params = array('hopelessX','admin'); $db->executeQuery(); //this contains a call to a private function that does the replace I can do it that way if I need to or I could make some overly complicated thing that finds each instance of the question mark, replaces it with a unique value and then adds that unique value to a replace array similarly to the method in code block two but that defeats to the purpose of making this simple database adapter in the first place (although it was mostly just for fun anyway). Anyway, if anyone has any advice and knows of a simple means to achieve this goal that would be much appreciated. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/237283-replace-each-instance-of-a-substring-with-values-from-an-array/ Share on other sites More sharing options...
anupamsaha Posted May 24, 2011 Share Posted May 24, 2011 Did you close the single quote correctly? $db->query = 'SELECT * FROM users WHERE user_name = :name AND user_role = :role'; // I have closed the single quote Quote Link to comment https://forums.phpfreaks.com/topic/237283-replace-each-instance-of-a-substring-with-values-from-an-array/#findComment-1219399 Share on other sites More sharing options...
.josh Posted May 24, 2011 Share Posted May 24, 2011 vsprintf should do the trick Good for if you have established positions of array values... $query = "SELECT * FROM users WHERE user_name = '%s' AND user_role = '%s'"; $query = vsprintf($query,array('name','role')); echo $query; If you need something more complex, for example, arbitrarily positioned values in the array, you can do something like this: $query = "SELECT * FROM users WHERE user_name = '%2\$s' AND user_role = '%1\$s'"; $query = vsprintf($query,array('role','name')); echo $query; Notice how the array has the values listed backwards, and in the query string, I'm specifying array elements (note: this acts more like regex's captured groups, starting at 1, not 0) Quote Link to comment https://forums.phpfreaks.com/topic/237283-replace-each-instance-of-a-substring-with-values-from-an-array/#findComment-1219411 Share on other sites More sharing options...
proggR Posted May 24, 2011 Author Share Posted May 24, 2011 vsprintf should do the trick Good for if you have established positions of array values... $query = "SELECT * FROM users WHERE user_name = '%s' AND user_role = '%s'"; $query = vsprintf($query,array('name','role')); echo $query; If you need something more complex, for example, arbitrarily positioned values in the array, you can do something like this: $query = "SELECT * FROM users WHERE user_name = '%2\$s' AND user_role = '%1\$s'"; $query = vsprintf($query,array('role','name')); echo $query; Notice how the array has the values listed backwards, and in the query string, I'm specifying array elements (note: this acts more like regex's captured groups, starting at 1, not 0) This is perfect. Thank you! I'll add this in today. I think I'll likely still support the more standard :placeholder style replace as well just to make the adapter more flexible but this is exactly what I was looking for to do what I wanted. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/237283-replace-each-instance-of-a-substring-with-values-from-an-array/#findComment-1219525 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.