Home > Backend Development > PHP Tutorial > Quick Start with PHP Regular Expressions

Quick Start with PHP Regular Expressions

PHP中文网
Release: 2016-07-15 13:27:38
Original
919 people have browsed it

PHP is a scripting language commonly used for writing dynamic WEB page development, and regular expressions are a very important part of PHP. Let’s talk about PHP regular expressions in detail. In ASP and ColdFusion, users can use a mixture of PHP and HTML to write WEB pages. When a visitor browses to the page, the server will first process the PHP commands in the page, and then transmit the processed results together with the HTML content to Access browser.

But unlike ASP or ColdFusion, PHP is an open source program with good cross-platform compatibility. Users can run PHP on Windows NT systems and many versions of Unix systems, and can run PHP as a built-in module of the Apache server or as a CGI program. In addition to being able to precisely control the display content of WEB pages, users can also send HTTP headers by using PHP. Users can set cookies through PHP, manage user identification, and redirect users' browsing pages. PHP has very powerful database support functions and can access almost all currently popular database systems. In addition, PHP can be integrated with multiple external libraries to provide users with more practical functions, such as generating PDF files. Although many people are already familiar with PHP regular expressions, as an important basic part, notes cannot be dispensed with.

Introduction to PHP regular expressions:

1. Square brackets

[0-9]匹配0-9  
[a-z]匹配a-z小写字母  
[A-Z]匹配A-Z大写字母  
[a-zA-Z]匹配所有大小写字母  
可以使用ascii来制定更多
Copy after login

2 .Quantifier

p+匹配至少一个含p的字符串  
p*陪陪任何包含0个或多个p的字符串  
p?匹配任何包含0个或一个p的字符串  
p{2}匹配包含2个p的序列的字符串  
p{2,3}匹配任何包含2个或3个的字符串  
p$匹配任何以p结尾的字符串  
^p匹配任何以p开头的字符串  
[^a-zA-Z]匹配任何不包含a-zA-Z的字符串  
p.p匹配任何包含p、接下来是任何字符、再接下来有又是p的字符串  
^.{2}$匹配任何值包含2个字符的字符串  
<b>(.*)b>匹配任何被<b>>包围的字符串  
p(hp)*匹配任何一个包含p,后面是多个或0个hp的字符串
Copy after login

3. Predefined character range

[:alpha:]同[a-zA-Z]  
[:alnum:]同[a-zA-Z0-9]  
[:cntrl:]匹配控制字符,比如制表符,反斜杠,退格符  
[:digit:]同[0-9]  
[:graph:]所有ASCII33~166范围内可以打印的字符  
[:lower:]同[a-z]  
[:punct:]标点符号  
[:upper:]同[A-Z]  
[:space:]空白字符,可以是空格、水平制表符、换行、换页、回车  
[:xdigit:]十六进制符同[a-fA-F0-9]
Copy after login

4. The ereg statement can be judged case-sensitively. In the following example,

if (ereg("([^a-z])","aaaaZaaaaaaa")) echo "不全是小写的!";
Copy after login

ereg can also return an array, such as

$url="//m.sbmmt.com";  
$a=ereg("^(http://www)\.([[:alnum:]]+)\.([[:alnum:]]+)",$url,$regs);  
echo $regs[0],"<br>";  
echo $regs[1],"<br>";  
echo $regs[2],"<br>";  
echo $regs[3],"<br>";
Copy after login

to get

//m.sbmmt.com
http://www  
php  
cn
Copy after login

If you still don’t understand, you can watch the Boolean Education Regular Expression Video Tutorial


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template