Replace text in quotation marks with placeholders

Hi everyone,

Happy New Year!!!

I’m looking for a way to replace the text in the quotation marks with placeholders.

Thus:

$string = "d.dog = 'poodle' AND t.tree = 'oak' ";

Becomes:

$string = "d.dog = ? AND t.tree = ? ";

Does anyone know how this can be done?

Thank you!

Welcome to the world of regex - http://php.net/manual/en/function.preg-replace.php
Also doing a search on php regex will give you help on the topic

I guess you need it to prepare a MySQL statement.
You can use mysqli classes for this purpose:
Suppose $stmt is an obect of calss mysqli_stmt, then you can prepare a query statement using $stmt->prepare, with question marks for placeholders, and then bind params using $stmt->bind_params.
There’s a wonderful example here.

Another way to use placeholders is to call functions such as printf and sprintf like in C.

$new_string = preg_replace("/'[^']*'/", '?', $string);
1 Like

Hey Drummin,

how are you doing? Good I hope. Thanks so much for the regular expression, it’s what I was looking for!

@Pepster, @Amit_Yaron,
thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.