Easy Tutorial
❮ Func Curl_File_Create Php Imagecolorexactalpha ❯

PHP preg_replace_callback() Function

PHP Regular Expressions (PCRE)

The preg_replace_callback function performs a regular expression search and replaces using a callback.

Syntax

mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] )

This function behaves similarly to preg_replace() except that it uses a callback for replacement string calculation instead of a fixed replacement string.

Parameter descriptions:

Return Value

If subject is an array, preg_replace_callback() returns an array; otherwise, it returns a string. It returns NULL on error.

If matches are found, it returns the modified subject string (or array of strings); otherwise, it returns the subject unchanged.

Example

Example 1

<?php
// Increment the year in the text by one.
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// Callback function
function next_year($matches)
{
  // Usually: $matches[0] is the complete match
  // $matches[1] is the match for the first captured group
  // and so on
  return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
            "|(\d{2}/\d{2}/)(\d{4})|",
            "next_year",
            $text);
?>

The execution result is as follows:

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

PHP Regular Expressions (PCRE)

❮ Func Curl_File_Create Php Imagecolorexactalpha ❯