truijens Posted October 21, 2013 Share Posted October 21, 2013 (edited) Hi all, I would like to parse a string containing key operator and value combinations like this: type=location&postdate>10-12-2013&published!=0 The first step is to explode it based on & of course. But I am looking for an effective way to parse the rest. I could use a if, ifelse, ifelse to search for the operator and then explode on that, like this: if (strpos('<=', $var) > 0) { $arr = explode('<=', $var); $operator = '<='; $key = $arr[0]; $value = $arr[1]; } ifelse (strpos('>=', $var) > 0) { $arr = explode('>=', $var); $operator = '>='; $key = $arr[0]; $value = $arr[1]; } etc... But that is very ugly. Do any of you have any suggestions? Maybe regex? Thanks in advance, Edited October 21, 2013 by truijens Quote Link to comment Share on other sites More sharing options...
kicken Posted October 21, 2013 Share Posted October 21, 2013 Use a regex of the possible operators. preg_split('#(>=?|<=?|!?=)#', $str); That would cover >, >=, <, <=, =, != Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted October 21, 2013 Share Posted October 21, 2013 Do you create those urls? You should only put these characters in a url, or else have them % encoded ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=. Quote Link to comment Share on other sites More sharing options...
truijens Posted October 21, 2013 Author Share Posted October 21, 2013 Use a regex of the possible operators. preg_split('#(>=?|<=?|!?=)#', $str); That would cover >, >=, <, <=, =, != Thank you! Exactly what I was looking for. Quote Link to comment Share on other sites More sharing options...
truijens Posted October 21, 2013 Author Share Posted October 21, 2013 (edited) Do you create those urls? You should only put these characters in a url, or else have them % encoded ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=. Nah, they won't be used as URL. A setting "string" somewhere, but yes, they are purposefully very similar to querystrings to make it a bit easier on our app builder. So everything is kind of the same/familiar to him. Edited October 21, 2013 by truijens Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.