PHP Regular Expression Application

Source: Internet
Author: User

Regular expressions

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

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

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

2. Split regular Expression, string

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

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

3. Match all strings that satisfy the regular

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

Preg_match_all ("/\d", $str, $arr); Regular expressions, defined strings, arrays

Var_dump ($arr); Match a string that satisfies all the regular strings

4. Match the first string that satisfies a regular

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

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

Var_dump ($arr);

Generate random numbers
echo rand ();
echo "<br>";
echo rand (0,10);
echo "<br>";
Get current time
echo Time ();
echo "<br>";
Format the display of time
echo Date ("Y-m-d h:i:s", "1506494423");
echo "<br>";
Echo strtotime ("2017-09-27 14:40:23");

String functions
Go string length
$str 1= "as";
$str 2= "ABCDECDCD";
Compare length ASC Code
Var_dump (strcmp ($str 2, $str 1));
Display length
echo strlen ($STR);


Splitting a string
Split ()--js
echo "<br>";
Var_dump (Explode (' B ', $str 2));
Link character
echo Implode (' | ', ["a", "B", "C"]);
Replace string lookup substitution
Echo str_replace ("CD", "CD", $str 2);
Specify location substitution
Echo Substr_replace ($str 2, "xxx", 0, 3);
Intercept string
Echo substr ($str 2,1,3);

PHP Regular Expression Application

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.