closure_048.phpt 594 B

1234567891011121314151617181920212223242526
  1. --TEST--
  2. Closure 048: Use in preg_replace_callback() using variables by reference
  3. --FILE--
  4. <?php
  5. function replace_variables($text, $params) {
  6. $c = function($matches) use (&$params, &$text) {
  7. $text = preg_replace( '/(\?)/', array_shift( $params ), $text, 1 );
  8. };
  9. preg_replace_callback( '/(\?)/', $c, $text );
  10. return $text;
  11. }
  12. echo replace_variables('a=?', array('0')) . "\n";
  13. echo replace_variables('a=?, b=?', array('0', '1')) . "\n";
  14. echo replace_variables('a=?, b=?, c=?', array('0', '1', '2')) . "\n";
  15. echo "Done\n";
  16. ?>
  17. --EXPECT--
  18. a=0
  19. a=0, b=1
  20. a=0, b=1, c=2
  21. Done