Reputation: 774
I want to explode a string like in SQL
this is my code:
<?php
$subject = 'select onlineid from onlinelist where online_user_name=6565 order by htrdh limit 1';
preg_match('/^(select|SELECT) (?P<select>(.+)) (from|FROM) (?P<from>(.+)) ((where|WHERE) (?P<where>(.+))) ((order by|ORDER BY) (?P<order>(.+)))? ((limit|LIMIT) (?P<limit>(.+)))?$/', $subject, $matches);
echo @$matches["select"]."<br>".@$matches["from"]."<br>".@$matches["where"]."<br>".@$matches["order"]."<br>".@$matches["limit"];
outPut will be :
onlineid
onlinelist
online_user_name=6565
htrdh
1
workes well...
But if I remove order by
or limit
it does not work.
it must if where
, order by
or limit
exists in $subject
put value
Upvotes: 2
Views: 1327
Reputation: 342
Regex is tricky and you should always be careful of using it unless you know every aspect of it. I would also suggest using /i for case insensitive
https://www.regex101.com/r/hM9aC7/1
/^select\s+(?<columns>.*?)\s+from\s+(.*?)?((where\s+(?<where>.*?))?(order by\s+(?<order>.*?))?(limit\s+(?<limit>(.*?)))?);/i
Upvotes: 1
Reputation: 192
Your ?
did not include the spaces.
Incorrect: ... (ORDER BY ...)? (LIMIT ...)?
Correct: ...( ORDER BY ...)?( LIMIT ...)?
https://www.regex101.com/r/mE8qK5/1
I also recommend that you add ?
to the .+
subpatterns so that it won't match greedily (lazy)
What do lazy and greedy mean in the context of regular expressions?
Upvotes: 1