开发者

PHP Post name wildcard - $_POST['var_'.*];

Is there some kind of wildcard I can use to create POST vars that start with a specific string?

Essentially, I am trying to capture any POST that contains bSortable_(wildcard) as the name.

bSortable_1 bSortable_2 bSortable_3

I am doing this for a library jquery datatabl开发者_开发问答es.

http://www.datatables.net/usage/server-side


Get post variables to an array, than iterate through it while you filter.

foreach($_POST as $key => $value) {
  $pos = strpos($key , "bSortable_");
  if ($pos === 0){
    // do something with $value
  }
}


Use name="bSortable[name_here]" or some permutation thereof. PHP can accept multidimensional POST parameters.

If that's not possible, you're going to have to loop though your available POSTed variables, and check the key names, like so:

<?php foreach($_POST as $key => $val) if(strpos($key, 'bSortable_') === 0) .... ?>


Yo want to create or read ? not clear your question.

to read, here it is my PHP 5.3 solution

$sortables = array_filter($_POST, function($e){
  return preg_match('/^bSortable/', $e);
});


You can also do this:

foreach($_POST as $key => $value){
    if(substr($key,0,10) == 'bSortable_'){
        //Do something here with the $value.
    }
}

Now you can make sure it does get the first part of the POST key, and not somewhere in the middle of key.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜