public function testManualNotification() { $this->markTestSkipped('Uncomment this skip call if you want to extract user agents into JSON'); $data = file_get_contents('tests/temp_data/access_log.txt'); $agents = []; mb_ereg_replace_callback('"(Mozilla.*?)"', function ($matches) use(&$agents) { //echo "match: " . $matches[1] . "\n\n\n"; array_push($agents, $matches[1]); return $matches[0]; }, $data); file_put_contents('tests/temp_data/access_log_agents_json.json', json_encode($agents)); }
<?php $str = 'abc 123 #",; $foo'; echo mb_ereg_replace_callback('(\\S+)', function ($m) { return $m[1] . '(' . strlen($m[1]) . ')'; }, $str);
/** * Match everything that looks like a timestamp and convert it to a human readable date-time format. * * @param string $output * @return string */ private static function translateTimestamp($output) { $regex_encoding = mb_regex_encoding(); $output = \mb_ereg_replace_callback('int[\\(| ]([0-9]{10})\\)?', function ($e) { if ($e[1] < mktime(0, 0, 0, 1, 1, 2000) || $e[1] > mktime(0, 0, 0, 1, 1, 2020)) { return $e[0]; } return $e[0] . ' <== ' . date('Y-m-d H:i:s', $e[1]); }, $output); if ($output === false) { throw new Exception\ErrorException('PCRE error ocurred while attempting to replace timestamp values with human-friedly format.'); # var_dump( array_flip(get_defined_constants(true)['pcre'])[preg_last_error()] ); } mb_regex_encoding($regex_encoding); return $output; }
/** * Updates irish Mac & Mc. * * @param string $string * * @return string */ private static function updateMac($string) { $string = mb_ereg_replace_callback('\\b(Ma?c)([A-Za-z]+)', function ($matches) { return $matches[1] . mb_strtoupper(mb_substr($matches[2], 0, 1)) . mb_substr($matches[2], 1); }, $string); // Now fix "Mac" exceptions foreach (self::$exceptions as $pattern => $replacement) { $string = mb_ereg_replace($pattern, $replacement, $string); } return $string; }
<?php // this text was used in 2002 // we want to get this up to date for 2003 $text = "April fools day is 04/01/2002\n"; $text .= "Last christmas was 12/24/2001\n"; echo mb_ereg_replace_callback("(\\d{2}/\\d{2}/)(\\d{4})", function ($matches) { return $matches[1] . ($matches[2] + 1); }, $text);
/** * Replace subject by callback or by string. * * @param string $pattern Pattern * @param callable|string $replacement Replacement * @param string $subject Subject * @param string $option Option * @return string Replaced subject */ private static function execReplace($pattern, $replacement, $subject, $option) { return (\is_object($replacement) || \is_array($replacement)) && \is_callable($replacement) ? \mb_ereg_replace_callback($pattern, $replacement, $subject, $option) : \mb_ereg_replace($pattern, $replacement, $subject, $option); }
/** * @iterations 10000 * @group native */ public function nativeReplaceCallback() { $replacement = $this->replacement; $res = \mb_ereg_replace_callback($this->pattern, function () use($replacement) { return $replacement; }, $this->subject); return $res; }
/** * @example {{ javascript:path-to-file }} * @param $text * @return string */ public static function replaceCode($text) { return mb_ereg_replace_callback('{{\\s*([a-zA-Z]+):([a-zA-Z0-9_\\/-]+)\\s*}}', 'Code::rep', $text); }
<?php // this text was used in 2002 // we want to get this up to date for 2003 $text = "April fools day is 04/01/2002\n"; $text .= "Last christmas was 12/24/2001\n"; // the callback function function next_year($matches) { // as usual: $matches[0] is the complete match // $matches[1] the match for the first subpattern // enclosed in '(...)' and so on return $matches[1] . ($matches[2] + 1); } echo mb_ereg_replace_callback("(\\d{2}/\\d{2}/)(\\d{4})", "next_year", $text); /** * Expected output: * April fools day is 04/01/2003 * Last christmas was 12/24/2002 */
/** * Substitui as ocorrências de uma string identificada por uma expressão regular utilizando uma função de callback. * * Este método funciona com strings multibyte. * * @access public * @param string $string Texto a ser alterado * @param string $regex Expressão regular de busca. Para fins de uniformização com outras funções de * expressão regular, a expressão deve ser passada com o delimitador (normalmente, /<regex>/). * @param callable $callback Função de callback que será chamada passando um array dos elementos encontrados dentro * do texto a ser alterado. * @param string $opcoes Modificadores da expressão regular de busca. * @return string Texto substituído. */ public function regexSubstituirCallback(string $string, string $regex, callable $callback, string $opcoes = self::REGEX_POSIX . self::REGEX_MODE_RUB) : string { return mb_ereg_replace_callback($regex, $callback, $string, $opcoes); }