Which function is used to replace text in a string in PHP?

Solution:

You can use preg_replace() instead of writing a custom function to modify your SQL filter:


$sqlFilter = "column = 'value'"; // Example filter

$newSqlFilter = preg_replace('/([^(\s]*?)[ ]{0,}=/', 'post.\0', $sqlFilter);

echo $newSqlFilter;
// Output: post.column = 'value'

✅ Explanation:

([^(\s]*?)[ ]{0,}= → matches any column name followed by optional spaces and an equal sign.

post.\0 → prepends post. to the matched column name while keeping the equal sign intact.

⚠️ Important: Make sure your query string is properly closed with a double quote (“) or single quote (‘) to avoid syntax errors.