Delete a desired entry on a post preg_replace

How do I get a query with a regular expression in the form of?

$data = "select * from table where id=5 AND ( round(data) = 2 ) AND ( round(data) = 5 ) AND FIND_IN_SET('159',data)";
$data = preg_replace("/AND ( round(data) = \d+ )/", "", $data);
$data = preg_replace("/AND FIND_IN_SET('\d+',data)/", "", $data);

Output:

select * from table where id=5

Upvotes: 0

Views: 40

Answers (1)

AndrewShmig
AndrewShmig

Reputation: 4923

Here is a working version of your code:

$data = "select * from table where id=5 AND ( round(data) = 2 ) AND ( round(data) = 5 ) AND FIND_IN_SET('159',data)";
$data = preg_replace("/AND \( round\(data\) = \d+ \)/", "", $data);
$data = preg_replace("/AND FIND_IN_SET\('\d+',data\)/", "", $data);

It was not working as expected because you forgot to place \ before ( and ). From PHP docs: http://php.net/manual/en/function.preg-replace.php

PHP treats ( and ) as blocks for matches.

Upvotes: 1

Related Questions