0

I would like to apply a function to every PHP POST or GET variable:

$_GET["some_variable"] -> some_function($_GET["some_variable"])

$_POST["some_variable"] -> some_function($_POST["some_variable"])

Any suggestion for the regex expression to match and replace those occurrences in my files?

8
  • 1
    Wait - do you want to modify your code, or just call a function on every $_GET/$_POST key? Modifying the code could be risky, as it could result in attempting to write to function calls or various other undesirable or syntactically invalid behaviors, like function calls suddenly appearing inside double-quoted strings where they can't execute. Commented Nov 14, 2014 at 17:40
  • I think he/she wants to modify the code. Commented Nov 14, 2014 at 17:41
  • Don't do that. The next person or the future you will hate you for that. Changing those super globals is E_BAAAAD. Commented Nov 14, 2014 at 17:42
  • Also I am fairly certain this is an XY problem. Commented Nov 14, 2014 at 17:45
  • Pretty much any decent IDE will have built in functions to allow you to do search/replace across all files in a library. Commented Nov 14, 2014 at 17:48

3 Answers 3

2

You can use array_map function to call a function on each element of an array:

$modifiedGet = array_map(some_function, $_GET);

$modifiedPost = array_map(some_function, $_POST);
Sign up to request clarification or add additional context in comments.

1 Comment

@user2703038, array_map is not recursive to my knowledge, so if you might be passing in arrays (e.g. checkboxes or multi-select), then it might not work for your particular case.
0

This is a bad idea. Consider some code like

<?php
   $_POST['override'] = 'some override value';

Unless you want to teach your replacement function PHP syntax rules, you'll end up with

<?php
    some_function($_POST['override']) = 'some override value';

which is a syntax error: Can't use function return value in write context.

1 Comment

This doesn't seem to apply. The OP wasn't trying to do a search/replace of code...just overwriting the value in the get|post.
-1

If you're going to downvote say why in the comment so I can fix a mistake if there is one. To my knowledge, this is a better method to call than the accepted answer since it is recursive and will work on sub-arrays. If it's not, I'd like to know why.

You can use array_walk_recursive to call a function for each value in an array:

function walk_function(&$value) {
    $value = some_function($value);
}
array_walk_recursive($_GET, 'walk_function');
array_walk_recursive($_POST, 'walk_function');

http://php.net/manual/en/function.array-walk-recursive.php

This has an advantage over array_map in that it is recursive and will call the function for arrays within the array. For example, if you submit checkboxes or multi-select there will be sub-arrays that may need the same processing done on them.

8 Comments

You can't assume what is the target type of some_function.
@CasimiretHippolyte, I've defined some_function, does that suffice?
Sorry, but no. You don't know what some_function is. some_function is only know by the OP, you can't assume what it is, and you need to build an answer with this fact.
@CasimiretHippolyte, ah, so what you're saying is if the questioner uses the function foo, even though it is a generic placeholder, from then on I have to assume that foo() is a real method and I can't use it for my own methods and have to use bar. I'll try to remember that for future reference. Thx.
Exactly, you don't know what some_function is, and you don't know what it does and what are the types of the array items (are they numbers, strings, arrays, objects? If they are arrays, must be each levels changed or not). To be short, you can't assume that a possible multidimensionnal array must be changed for all its levels.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.