Regular functions are often used in php development.

Source: Internet
Author: User

$ Temp [0] = "abc ";
$ Temp [1] = 123;
$ Temp [2] = "us .";
$ Temp [3] = 5;
$ Temp [4] = 58;
$ Temp [5] = "cs ";
// The code above defines an array with both numbers and strings
Echo "the contents of the array temp are :";
Echo "<p> ";
// Output array content through loop
For ($ I = 0; $ I <count ($ temp); $ I ++)
{
Echo $ temp [$ I];
Echo ",";
}
Echo "<p> ";
Echo "the new array after processing by preg_grep () is :";
$ Temp2 = preg_grep ("/^ (d) d */", $ temp); // use preg_grep for processing
// In the above regular expression mode, "//" is the delimiter and its content is all integer elements.
Echo "<p> ";
Print_r ($ temp2); // outputs the content of the new array through loop

//

$ Str = 'string'; // define a string
$ Chars = preg_split ('//', $ str,-1, preg_split_no_empty); // use a regular expression to perform the split operation.
Print_r ($ chars); // output result array: array ([0] => s [1] => t [2] => r [3] => I [4] => n [5] => g)


//

$ String = "L 15,200 3"; // define a string
$ Pattern = "/(w +) (d +), (d +)/I"; // define the matching mode
$ Replacement = "$ {1} 1, $3"; // define the replacement content
Print preg_replace ($ pattern, $ replacement, $ string); // output result april1, 2003

//

$ Text = "maid day is 04/01/2002 n"; // define a string
$ Text. = "last christmas was 12/24/2001 n"; // String Update
$ Pattern = "| (d {2}/d {2}/) (d {4}) |"; // defines the matching mode
Function next_year ($ matches) // defines the callback function
{
Return $ matches [1]. ($ matches [2] + 1); // return value
}
$ Result = preg_replace_callback ($ pattern, "next_year", $ text); // use the callback function to perform search and replace
Echo $ result; // output result "maid was 12/24/2002"

//

$ Keywords = "'php '"; // define a string
$ Keywords = preg_quote ($ keywords, "'"); // Escape "'"
Echo $ keywords;
?>

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.