PHP mixed Preg_replace_callback Instance application code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags application code connections content expression function http link

PHP Tutorials Mixed Preg_replace_callback Instance application code

Requirements: Add a request=xxx after all connections; This function is more flexible than preg_replace, and note that it replaces the contents of the entire regular expression.
$content = ' <a href= ' http://www.jzread.com/aaa.php ' > Link 1</a><a href= ' http://www.jzread.com/aaa.php? id=111 "> Link 2</a>";
function Add_source ($matches)
{
if (Strpos ($matches [1], '? '))
{
Return ' href= '. $matches [1]. '  &request=xxx "'; Note that here and below are the things that are outside the regular brackets: href=.
}
Else
{
Return ' href= '. $matches [1]. Request=xxx "';
}
}
$content = Preg_replace_callback ('/href=[' |) (.*?) ['|"] /', ' Add_source ', $content);


Example Two


This text is used for 2002 years,
Now want to make it available for 2003
$text = "April Jesters day is 04/01/2002n";
$text. = "Last Christmas was 12/24/2001n";

callback function
function Next_year ($matches) {
Typically: $matches [0] is a complete match
$matches [1] is a match for the child pattern in the first parenthesis
So
return $matches [1]. ($matches [2]+1);
}

Echo Preg_replace_callback (
"| (d{2}/d{2}/) (D{4}) | ",
"Next_year",
$text);

The results are:
April Jesters Day is 04/01/2003
Last Christmas was 12/24/2002

Related Article

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.