Php Regular Expression application, php Regular Expression

Source: Internet
Author: User
Tags echo date php regular expression

Php Regular Expression application, php Regular Expression

Regular Expression

1. Replace "/\ d/", "#", $ str: Regular Expression \ d number, replace with #, string

$ Str = "2 hello 5li 6lei ";

Echo preg_replace ("/\ d/", "#", $ str );

 

2. Split the Regular Expression and string

$ Str = "2 hello 5li 6lei ";

Var_dump (preg_split ("/\ d/", $ str ));

3. Match All strings that meet the Regular Expression

$ Str = "2 hello 5li 6lei ";

Preg_match_all ("/\ d", $ str, $ arr); // regular expression, defined string, array

Var_dump ($ arr); // match strings that meet all the regular expressions

4. Match the first regular string

$ Str = "2 hello 5li 6lei ";

Preg_match ("/\ d/", $ str, $ arr );

Var_dump ($ arr );

 

Generate random number
Echo rand ();
Echo "<br> ";
Echo rand (0, 10 );
Echo "<br> ";
// Obtain the current time
Echo time ();
Echo "<br> ";
// Format the display time
Echo date ("Y-m-d H: I: s", "1506494423 ");
Echo "<br> ";
Echo strtotime ("14:40:23 ");

String Functions
Remove string length
$ Str1 = "";
$ Str2 = "abcdecdcd ";
// Compare the ASC code
Var_dump (strcmp ($ str2, $ str1 ));
// Display Length
Echo strlen ($ str );


// Split the string
// Split () -- js
Echo "<br> ";
Var_dump (explode ('B', $ str2 ));
// Link character
Echo implode ('|', ["a", "B", "c"]);
Replace string search and replace
Echo str_replace ("cd", "CD", $ str2 );
Specified position replacement
Echo substr_replace ($ str2, "xxx", 0, 3 );
Truncate string
Echo substr ($ str2, 1, 3 );

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.