Last active
January 16, 2016 16:21
-
-
Save BastienClement/61f001071bb8e640a917 to your computer and use it in GitHub Desktop.
Revisions
-
Bastien Clément revised this gist
Jan 16, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,13 +26,13 @@ function preg_replace_safe($search, $replace, $subject, $evaluator = NULL) { if (is_callable($r)) { $subject = preg_replace_callback($s, $r, $subject); } else { $stripped = preg_replace('/^(.)(.*\\1[a-z]*)e([a-z]*)$/si', "$1$2$3", $s); if ($stripped != $s) { $subject = preg_replace_callback($stripped, function($matches) use ($r, $evaluator) { $code = preg_replace_callback('/\\$(?|\{(\\d+)\}|(\\d+))/', function($m) use ($matches) { return addslashes($matches[(int) $m[1]]); }, $r); return $evaluator('return ' . $code . ';'); }, $subject); } else { $subject = preg_replace($s, $r, $subject); -
Bastien Clément revised this gist
Jan 16, 2016 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,9 @@ function preg_replace_safe($search, $replace, $subject, $evaluator = NULL) { if (is_null($evaluator)) { // A custom evaluator // function($c) { return eval($c); } // is required if the replacement code uses the $this reference. $evaluator = function($code) { return eval($code); }; } -
Bastien Clément revised this gist
Jan 16, 2016 . No changes.There are no files selected for viewing
-
Bastien Clément revised this gist
Jan 16, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ <?php function preg_replace_safe($search, $replace, $subject, $evaluator = NULL) { if (is_null($evaluator)) { $evaluator = function($code) { return eval($code); }; -
Bastien Clément created this gist
Jan 16, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ function preg_replace_safe($search, $replace, $subject, $evaluator = NULL) { if (is_null($evaluator)) { $evaluator = function($code) { return eval($code); }; } if (is_array($subject)) { return array_map(function($subject) use ($search, $replace) { return preg_replace_safe($search, $replace, $subject); }, $subject); } else { if (is_array($search) && is_array($replace)) { $replacements = array_map(function($a, $b) { return [$a, $b]; }, $search, $replace); } else if(is_array($search)) { $replacements = array_map(function($a) use($replace) { return [$a, $replace]; }, $search); } else { $replacements = [[$search, $replace]]; } foreach ($replacements as $replacement) { list ($s, $r) = $replacement; if (is_callable($r)) { $subject = preg_replace_callback($s, $r, $subject); } else { $stripped = preg_replace("/^(.)(.*\\1[a-z]*)e([a-z]*)$/si", "$1$2$3", $s); if ($stripped != $s) { $subject = preg_replace_callback($stripped, function($matches) use ($r, $evaluator) { $code = preg_replace_callback("/\\$(?|\{(\\d+)\}|(\\d+))/", function($m) use ($matches) { return addslashes($matches[(int) $m[1]]); }, $r); return $evaluator("return " . $code . ";"); }, $subject); } else { $subject = preg_replace($s, $r, $subject); } } } return $subject; } }