Jump to content

Effectively parsing a string containing key+operator+value combinations.


truijens

Recommended Posts

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 by truijens
Link to comment
Share on other sites

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 by truijens
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.