Ver Mensaje Individual
  #393 (permalink)  
Antiguo 23/05/2011, 19:52
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Juego: "¿Dónde está el error?"

Código PHP:
Ver original
  1. <?php
  2. function replace_words($list, $line, $callback) {
  3.     return preg_replace_callback(
  4.         '/(' . implode('|', array_map('preg_quote', $list)) . ')/is',
  5.         function($v) { return $callback($v[1]); },
  6.         $line
  7.     );
  8. }
  9. $list = array('foo', 'candy');
  10. $str = "foo bar baz candy fruits";
  11.  
  12. echo htmlentities(replace_words($list, $str, function($v) {
  13.     return "<strong>{$v}</strong>";
  14. }));
Para versiones de PHP 5.3.x, se supone que imprima
Cita:
<strong>foo</strong> bar baz <strong>candy</strong> fruits
Pero sale
Cita:
Notice: Undefined variable: callback in ... on line 5

Fatal error: Function name must be a string in ... on line 5
¿Por qué y como resolverlo sin usar variables globales?
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos