Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP preg_replace_callback() Function

PHP preg_replace_callback() Function

PHP Regular Expressions (PCRE)PHP Regular Expressions (PCRE)

The preg_replace_callback function performs a regular expression search and uses a callback for replacement.

Syntax


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

This function behaves identically to preg_replace() except that it allows you to specify a callback instead of replacement to compute the replacement string.

Parameter Description:

Return Value

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

If a match is found, returns the replaced target string (or array of strings); otherwise subject is returned unchanged.

Example

Example 1

The execution result is as follows:


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

PHP Regular Expressions (PCRE)PHP Regular Expressions (PCRE)