Ryan Claxton Ryan Claxton - 5 months ago 9
SQL Question

php string replace function

I need to write a function that takes in a long string that is a sql command and searches for an equal sign goes back to the word before it and adds "post." before it.

Example:

input:

$sqlFilter = "WHERE sport = 'Hockey' AND team = 'New York Rangers';


Output:

$sqlFilter = "WHERE post.sport = 'Hockey' AND post.team = 'New York Rangers';

Answer

You don't need to write any function because you have a preg_replace.

You need to add this code

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

And the other thing - close your query string with " character.