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<?php
include 'replace.php';
include 'replacer.php';
$rplc = new Replace(
'@\.([a-z_]+)@i',
'["$1"]',
'$color.name.lala'
);
echo "<br>replaced: " . $rplc->getReplaced();
$rplcr = new Replacer(
'@\.([a-z_]+)@i',
'["$1"]',
'$color.name.lala'
);
echo "<br>replacedR: " . $rplcr->getReplaced();
?>
replace.php
Código PHP:
Ver original<?php
class Replace
{
private $pattern = null;
private $replace = null;
private $subject = null;
private $capture = '@\$([0-9]{1,2})@i';
private $perform = 'self::callback';
public function __construct($pattern, $replace, $subject)
{
$this->pattern = $pattern;
$this->replace = $replace;
$this->subject = $subject;
if(!$this->is_match())
return false;
$this->pattern,
$this->perform,
$this->subject
);
}
private function callback($matches)
{
$replaced = null;
$callback = function($groups) use ($matches, &$replaced) {
$match = $matches[$groups[1]];
$groups[0],
$match,
$this->replace
);
};
$this->capture,
$callback,
$this->replace
);
$matches[0],
$replaced,
$this->subject
);
}
private function is_match()
{
$this->pattern,
$this->subject
);
return ($count !== 0)? true : false;
}
public function getReplaced()
{
return $this->subject;
}
}
?>
replacer.php
Código PHP:
Ver original<?php
class Replacer
{
private $pattern = null;
private $replace = null;
private $subject = null;
private $capture = '@\$([0-9]{1,2})@i';
private $perform = 'self::callback';
public function __construct($pattern, $replace, $subject)
{
$this->pattern = $pattern;
$this->replace = $replace;
$this->subject = $subject;
if(!$this->is_match())
return false;
$this->pattern,
$this->perform,
$this->subject
);
}
private function callback($matches)
{
$callback = function($groups) use ($matches) {
$match = $matches[$groups[1]];
$groups[0],
$match,
$this->replace
);
};
$this->capture,
$callback,
$this->replace
);
$matches[0],
$replaced,
$this->subject
);
}
private function is_match()
{
$this->pattern,
$this->subject
);
return ($count !== 0)? true : false;
}
public function getReplaced()
{
return $this->subject;
}
}
?>
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