字符串更改函数的质量传递?

For string functions, I find myself doing this kind of thing a lot:

$string = trim($string);

I would like to easily have functions where I can pass the string by reference, so I can instead do function($string);.

I was thinking about writing wrappers for all the string functions I want to use, and prefix it with a special character, like so:

 function _trim(&$string) {
   trim($string);
 }

 function _str_replace($a, $b, &$string) {
    str_replace($a, $b, $string);
 }

But I wonder if there's a way that's easier than writing a wrapper for each function? (Has someone written this library already?)

For functions that can take only one argument (the string) like trim(), strtolower() etc, you could do something like this maybe...?

function modify_str (&$str, $funcName) {
  if (function_exists($funcName)) $str = $funcName($str);
}

// So you can now do
modify_str($str, 'trim');
modify_str($str, 'strtolower');

...but to be perfectly honest, I cant really see the point - for one thing, you can't chain functions that take an argument by reference (i.e. you can't do trim(strtolower($str)))

I tend not to write $str = trim($str); very much, because I am probably going to use the result fairly immediately in some other operation. Instead of this:

$str = trim($str);
some_func($str, $someOtherVar);

I just do this:

some_func(trim($str), $someOtherVar);

And in the event that I need to do something like this:

$str = trim($str);
some_func($str, $someOtherVar);
another_func($str, $yetAnotherVar);

I would do this:

some_func($str = trim($str), $someOtherVar);
another_func($str, $yetAnotherVar);

...but whatever you are trying to do by doing any of the above, what you are effectively achieving is making your code less readable, and probably nothing else.

EDIT

After doing something completely unrelated today, I realised there is a way to chain functions and call it by reference:

function modify_str (&$str) {
  $args = func_get_args();
  for ($i = 1; isset($args[$i]); $i++) {
    if (function_exists($funcName = $args[$i])) $str = $funcName($str);
  }
}

// So you could do
modify_str($str,'trim','strtolower');

...but I still maintain this is not the way to go.

ANOTHER EDIT

You could pass to functions that uses multiple arguments by doing something like this (untested):

function modify_str (&$str) {
  $str_identifier = '$$$'; // This identifies the argument where $str should be used
  $ops = func_get_args();
  for ($i = 1; isset($args[$i]); $i++) { // Loop functions to be applied
    if (function_exists($ops[$i]['function'])) {
      $args = array();
      for ($j = 0; isset($ops[$i][$j]); $j++) { // Loop arguments for this function and build an argument list in PHP syntax
        $args[] = ($ops[$i][$j] === $str_identifier) ? '$str' : "\$ops[\$i][$j]";
      }
      // eval() it (as if you had written it as straight PHP)
      eval("\$str = {$ops[$i]['function']}(".implode(',',$args).");");
    }
  }
}

// So you can call it like this
modify_str($str,array('function'=>'str_replace','&','&','$$$'),array('function'=>'explode',"
",'$$$'));
// ..which should have the same effect as
$str = explode("
",str_replace('&','&',$str));

// As you can see, the not-by-reference way is is much shorter and more readable