Jump to content

Replace Each Instance of a substring with values from an array.


proggR

Recommended Posts

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.