Ver Mensaje Individual
  #1 (permalink)  
Antiguo 27/07/2015, 03:19
Avatar de Fernand0
Fernand0
 
Fecha de Ingreso: septiembre-2005
Ubicación: Buenos Aires
Mensajes: 610
Antigüedad: 18 años, 7 meses
Puntos: 19
preg_replace_callback bug?

Buenas, tengo un problema medio raro con el preg_replace_callback.. pego el codigo y el output para que lo vean...

(viendo el index.php y el output se ve que quiero lograr)

index.php
Código PHP:
Ver original
  1. <?php
  2.    
  3.     include 'replace.php';
  4.     include 'replacer.php';
  5.    
  6.     $rplc = new Replace(
  7.         '@\.([a-z_]+)@i',
  8.         '["$1"]',
  9.         '$color.name.lala'
  10.     );
  11.    
  12.     echo "<br>replaced: " . $rplc->getReplaced();
  13.    
  14.     $rplcr = new Replacer(
  15.         '@\.([a-z_]+)@i',
  16.         '["$1"]',
  17.         '$color.name.lala'
  18.     );
  19.    
  20.     echo "<br>replacedR: " . $rplcr->getReplaced();
  21.    
  22. ?>

replace.php
Código PHP:
Ver original
  1. <?php
  2.        
  3.         class Replace
  4.         {
  5.            
  6.             private $pattern = null;
  7.            
  8.             private $replace = null;
  9.            
  10.             private $subject = null;
  11.            
  12.             private $capture = '@\$([0-9]{1,2})@i';
  13.            
  14.             private $perform = 'self::callback';
  15.            
  16.            
  17.             public function __construct($pattern, $replace, $subject)
  18.             {
  19.                
  20.                 $this->pattern = $pattern;
  21.                 $this->replace = $replace;
  22.                 $this->subject = $subject;
  23.                
  24.                 if(!$this->is_match())
  25.                     return false;
  26.                
  27.                 preg_replace_callback(
  28.                     $this->pattern,
  29.                     $this->perform,
  30.                     $this->subject
  31.                 );
  32.                
  33.             }
  34.            
  35.             private function callback($matches)
  36.             {
  37.                
  38.                 $replaced = null;
  39.                
  40.                 $callback = function($groups) use ($matches, &$replaced) {
  41.                    
  42.                     $match = $matches[$groups[1]];
  43.                    
  44.                     $replaced = str_replace(
  45.                         $groups[0],
  46.                         $match,
  47.                         $this->replace
  48.                     );
  49.                    
  50.                 };
  51.                
  52.                 preg_replace_callback(
  53.                     $this->capture,
  54.                     $callback,
  55.                     $this->replace
  56.                 );
  57.                
  58.                 $this->subject = str_replace(
  59.                     $matches[0],
  60.                     $replaced,
  61.                     $this->subject
  62.                 );
  63.                
  64.             }
  65.            
  66.             private function is_match()
  67.             {
  68.                
  69.                 $count = preg_match(
  70.                     $this->pattern,
  71.                     $this->subject
  72.                 );
  73.                
  74.                 return ($count !== 0)? true : false;
  75.                
  76.             }
  77.            
  78.             public function getReplaced()
  79.             {
  80.                
  81.                 return $this->subject;
  82.                
  83.             }
  84.            
  85.         }
  86.    
  87. ?>

replacer.php
Código PHP:
Ver original
  1. <?php
  2.    
  3.         class Replacer
  4.         {
  5.            
  6.             private $pattern = null;
  7.            
  8.             private $replace = null;
  9.            
  10.             private $subject = null;
  11.            
  12.             private $capture = '@\$([0-9]{1,2})@i';
  13.            
  14.             private $perform = 'self::callback';
  15.            
  16.            
  17.             public function __construct($pattern, $replace, $subject)
  18.             {
  19.                
  20.                 $this->pattern = $pattern;
  21.                 $this->replace = $replace;
  22.                 $this->subject = $subject;
  23.                
  24.                 if(!$this->is_match())
  25.                     return false;
  26.                
  27.                 $this->subject = preg_replace_callback(
  28.                     $this->pattern,
  29.                     $this->perform,
  30.                     $this->subject
  31.                 );
  32.                
  33.             }
  34.            
  35.             private function callback($matches)
  36.             {
  37.                
  38.                 $callback = function($groups) use ($matches) {
  39.                    
  40.                     $match = $matches[$groups[1]];
  41.                    
  42.                     return str_replace(
  43.                         $groups[0],
  44.                         $match,
  45.                         $this->replace
  46.                     );
  47.                    
  48.                 };
  49.                
  50.                 $replaced = preg_replace_callback(
  51.                     $this->capture,
  52.                     $callback,
  53.                     $this->replace
  54.                 );
  55.                
  56.                 return str_replace(
  57.                     $matches[0],
  58.                     $replaced,
  59.                     $this->subject
  60.                 );
  61.                
  62.             }
  63.            
  64.             private function is_match()
  65.             {
  66.                
  67.                 $count = preg_match(
  68.                     $this->pattern,
  69.                     $this->subject
  70.                 );
  71.                
  72.                 return ($count !== 0)? true : false;
  73.                
  74.             }
  75.            
  76.             public function getReplaced()
  77.             {
  78.                
  79.                 return $this->subject;
  80.                
  81.             }
  82.            
  83.         }
  84.    
  85. ?>

output:
Código HTML:
replaced: $color["name"]["lala"]
replacedr: $color$color["["name"]"].lala$color.name["["lala"]"]

Por alguna razon, el codigo correcto deberia ser el de "replacer.php", pero como ven en el output, devuelve cualquier cosa..

Tuve que dejar de usar el return en el preg_replace_callback.. y directamente en el callback asignarle el valor a parseado a una variable.

Si me dan una ayuda se los agradeceria