closure_047.phpt 582 B

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