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,

Do you create those urls?

 

You should only put these characters in a url, or else have them % encoded

 

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.