PHP.nl

mb_ereg_replace_callback

mb_ereg_replace_callback

Perform a regular expression search and replace with multibyte support using a callback

 **mb_ereg_replace_callback** string $pattern callable $callback string $string  $options

Scans for matches to , then replaces the matched text with the output of function.
string``pattern``callback

The behavior of this function is almost identical to , 
except for the fact that instead of
 parameter, one should specify a
.

mb_ereg_replace``replacement``callback

patternThe regular expression pattern.

   Multibyte characters may be used in .
  `pattern`

callback A callback that will be called and passed an array of matched elements in the string. The callback should return the replacement string. string

    You'll often need the  function
    for a  in just one place.
    In this case you can use an
     to
    declare the callback within the call to
    . By doing it this way
    you have all information for the call in one place and do not
    clutter the function namespace with a callback function's name
    not used anywhere else.        
  `callback``mb_ereg_replace_callback`anonymous function`mb_ereg_replace_callback`

string The being checked. string

options The search option. See for explanation. mb_regex_set_options

The resultant on success, or false on error. If is not valid for the current encoding, null is returned. string``string

Voorbeeld: example

<?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);

?>
April fools day is 04/01/2003
Last christmas was 12/24/2002

Voorbeeld: using anonymous function

<?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);
?>

mb_regex_encoding``mb_ereg_replaceAnonymous functions