Phpmixedpreg_replace_callback instance application code _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.