Phpmixedpreg_replace_callback instance application code. Php Tutorial mixedpreg_replace_callback instance application code requirements: add a requestxxx after all connections; this function is more flexible than preg_replace. Note that it replaces the php Tutorial mixed preg_replace_callback instance application code
// Requirement: add a request = xxx after all connections. This function is more flexible than preg_replace. Note that it replaces the content of the entire regular expression.
$ Content = 'http: // www.bkjia.com/aaa.php? Id = 111 "> Link 2 ';
Function add_source ($ matches)
{
If (strpos ($ matches [1], '? '))
{
Return 'href = "'. $ matches [1]. '& request = xxx "'; // Note: here and below are all added with something out of the regular brackets: href ="
}
Else
{
Return 'href = "'. $ matches [1].'? Request = xxx "';
}
}
$ Content = preg_replace_callback ('/href = [' | "] (. *?) ['| "]/', 'Add _ source', $ content );
// Instance 2
// This article was used in 2002,
// Now you want to use it for January 1, 2003
$ Text = "maid day is 04/01/2002 n ";
$ Text. = "last christmas was 12/24/2001 n ";
// Callback function
Function next_year ($ matches ){
// Generally: $ matches [0] is a complete match.
// $ Matches [1] is the matching item of the child pattern in the first bracket.
// And so on
Return $ matches [1]. ($ matches [2] + 1 );
}
Echo preg_replace_callback (
"| (D {2}/d {2}/) (d {4}) | ",
"Next_year ",
$ Text );
// The result is:
// Else L fools day is 04/01/2003
// Last christmas was 12/24/2002
Required mixed preg_replace_callback instance application code // requirement: add a request = xxx after all connections; this function is more flexible than preg_replace. Note that it is replaced...