tags:

views:

41

answers:

2

I'm writing a bot for the IMified network.

I want to filter the items processed by my bot by only accepting certain values into my script.

This is what I'm using right now:

$items = array('botkey', 'userkey', 'network', 'user', 'channel', 'msg', 'step');

foreach ($_POST as $key => $value)
{
    if (in_array($key, $items) || preg_match('value\d*', $key))
    {
     $this->data[$key] = $value;
    }
}
  1. Is there any way to do this better?
  2. Why doesn't my regular expression work – it is meant to only allow keys named like value1234 or anything similar ("value" + number), but it doesn't let anything through.
+1  A: 

i think it should be preg_match('/value\d*/', $key)

i think thats a pretty good approach to the given problem

Sabeen Malik
+2  A: 
  1. There's always a better way, but -- as long as you put some taint/sanity checking into your code for the values, you're doing just fine.

  2. Your regular expression syntax is wrong. Try: preg_match('/^value\d+$/', $key)

joealba
+1 Almost exactly what I was typing.
timdev
Awesome, thanks so much.
abrahamvegh