开发者

Validating parentheses for a MySQL query

开发者 https://www.devze.com 2023-03-26 23:52 出处:网络
I\'m giving my users the ability to create their own MySQL queries. Don\'t worry, I\'m taking all the sql injection etc into account.

I'm giving my users the ability to create their own MySQL queries. Don't worry, I'm taking all the sql injection etc into account.

What I am wanting to do is validate the parentheses they use while creating their query...

So lets say they their query looks like this:

$string = '1 AND (2 OR 3) AND (4 AND (5 OR 6)';

See how they开发者_运维问答 didn't close the open parenthesis before the number 4? Is there any regex I could run on this to validate any open or closed parentheses that haven't been added correctly?

I've been playing and searching everywhere but I have no idea how to make this work.


Thanks @Jayantha! Here's the solution I found from clicking your link:

$string = '1 AND (2 OR 3) AND (4 AND (5 OR 6)';

if (!preg_match("/^((?:[^()]|\((?1)\))*+)$/", $string, $matches))
    echo 'Your parentheses are wrong!';
0

精彩评论

暂无评论...
验证码 换一张
取 消