1

I am working on a project and I can't figure out how to go about doing it. I'm trying to do with Regex but fairly new to it.

I have a string such as WHERE MyColumn='1's'' AND MyColumn='Test's''"

I have the following regex in PHP

$found = array();
preg_match("/\s=\s'.*'|\s.*='.*'\s/", $whereQuery, $found);

In my array I have the following

Array
(
    [0] =>  MyColumn='1's'' 
)

So it's almost there, except I am expecting the following:

Array
(
    [0] =>  MyColumn='1's'' 
    [1] =>  MyColumn='Test's'
)
2
  • preg_match only return first match. If want multiple matches, use preg_match_all. Commented Aug 25, 2016 at 19:03
  • Use an SQL parser. Stop to try to parse the SQL language with regex, it's a waste of time. Commented Aug 25, 2016 at 21:09

1 Answer 1

1

You should use preg_match_all with this simplified regex:

preg_match_all('/\w+\h*=\h*\S+/', $whereQuery, $found);

RegEx Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Wow that is much simpler. Works great

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.