0

I saw another post suggesting using this statement to trim string variables contained in the array:

$_POST=array_map('trim', $_POST);

However, if in the first place, the strings are not contained in an array, I would like to have a trim function that can be used like this:

$a='  aaa ';
$b='  bbb ';
$c='  ccc ';
trimAll($a,$b,$c); //arbitrary number of string variables can be passed

I tried to write a function like this:

function trimAll() {

    $args = &func_get_args();
    foreach($args as &$arg) {
        if(isset($arg) && is_string($arg))
            $arg=&trim($arg);
    }
      //no return value is required
}

But without success, the string variables do not get trimmed after function return.

Why and how can this be done??

bobo
  • 8,439
  • 11
  • 57
  • 81
  • Will you need to do `trimAll(&$a, &$b);` ? – alex Oct 29 '09 at 00:54
  • possible duplicate of [PHP: variable-length argument list by reference?](http://stackoverflow.com/questions/1925253/php-variable-length-argument-list-by-reference) – newacct Oct 07 '11 at 09:20

6 Answers6

4

you cannot pass variable number of parameters by reference. As a workaround, try something like

list($a, $b, $c) = array_map('trim', array($a, $b, $c));

better yet, rewrite the snippet so that it doesn't require to use a bunch of variables, which is a bad idea anyways

user187291
  • 53,363
  • 19
  • 95
  • 127
2

This also works, but will likely make anyone you might happen to work with frustrated as its very unintuitive:

// pass variables by string name
extract(array_map('trim', compact('a', 'b', 'c')));
ken
  • 3,650
  • 1
  • 30
  • 43
1

I don't think you can pass a variable-length list of args by reference.

You could pass in an array of references.

function trimAll($array) {
    foreach($array as $k => $v) {
        if(isset($array[$k]) && is_string($array[$k]))
            $array[$k]=&trim($array[$k]);
    }
}

... and suitably modify your call to create an array of references.

$a='  aaa ';
$b='  bbb ';
$c='  ccc ';
trimAll(array(&$a,&$b,&$c));
martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • If you add prefix the argument $array in the function trimAll with &, will that then enable you to use it normally – alex Oct 29 '09 at 01:15
  • It won't, it complains Fatal error: Only variables can be passed by reference on line 15 http://codepad.org/FItzMVjn – bobo Oct 29 '09 at 01:22
  • 1
    maybe I should adopt approach's stereofrog – bobo Oct 29 '09 at 01:33
  • @ken: this is not call-time pass by reference. putting references in an array is specifically allowed – newacct Oct 07 '11 at 09:16
1

I'm not convinced that this is possible using func_get_args, though a comment on it's PHP manual page suggests one possible alternative solution: http://uk3.php.net/manual/en/function.func-get-args.php#90095

However user187291's workaround looks far simpler.

Shog9
  • 156,901
  • 35
  • 231
  • 235
PeterJCLaw
  • 1,862
  • 1
  • 16
  • 20
0

Have you tried passing in the variables by Reference.

trimAll(&$a,&$b,&$c)
Toby Allen
  • 10,997
  • 11
  • 73
  • 124
  • 1
    Call-time pass-by-reference (what you suggest) is deprecated in PHP 5. – ken Oct 29 '09 at 01:27
  • I know its depreciated, but is it not also about the only way he can do what he wants unless there is a way of declaring that all (unknown number of) properties of a method will be passed byref – Toby Allen Oct 29 '09 at 17:20
  • This will still not work with the `trimAll` function as it is written (have you tried it?) – newacct Oct 07 '11 at 09:24
0

This works, but uses call-time pass-by-reference, which is deprecated in PHP 5.3:

function trimAll() {
    $backtrace = debug_backtrace();
    foreach($backtrace[0]['args'] as &$arg)
        if(isset($arg) && is_string($arg))
            $arg=trim($arg);
}
$a='  aaa ';
$b='  bbb ';
$c='  ccc ';
trimAll(&$a,&$b,&$c);
echo "$a\n";
echo "$b\n";
echo "$c\n";
newacct
  • 119,665
  • 29
  • 163
  • 224