search
HomeBackend DevelopmentPHP TutorialDetailed explanation of basic usage examples of regular expressions in PHP introductory tutorial

本文实例讲述了PHP正则表达式基本用法。分享给大家供大家参考,具体如下:

Demo1.php

<?php
 //尝试着写第一个正则表达式
 //第一个参数,表示模式(就是和字符串去比较,匹配)
 //第二个参数,表示字符串
 //如果整个函数模式和字符串比较后匹配了,那么返回真(true)1,否则返回假(false)0
 //什么叫匹配,就是一个一个的比较过去
 //按照什么规则??按照模式来匹配
 //只要模式全部通过,那么就通过
 //匹配和相等是两个概念
 //因为目前的模式是一个整体,php
 //所以,字符串只要有连在一起的 php 三个字符,不管怎样的字符串都能匹配
 $mode = &#39;/php/&#39;; //规则模式
 $string = &#39;fsdfsdfsdphpfsdfs&#39;; //字符串
 //echo preg_match($mode,$string);
 if( preg_match($mode,$string)){
 echo &#39;匹配&#39;;
 }else{
 echo &#39;不匹配&#39;;
 }
 //echo preg_match(&#39;/php/&#39;,&#39;php&#39;);
// if(&#39;a&#39;==&#39;a&#39;){
// echo &#39;相等&#39;;
// }else {
// echo &#39;不相等&#39;;
// }
?>

Demo2.php

<?php
 //什么叫前导,前面的一个字符
 //+ 号的前导字符就是 h
 //h+ 的意思是,至少包含一个 h
 //+ 号的意思
 //$mode = &#39;/ph+p/&#39;; //规则模式
 //h* 的意思是,零个或者多个 h
 //* 号虽然可以是零个,但是前导字符不能更改,更改了,就不匹配了
 //$mode = &#39;/ph*p/&#39;; //规则模式
 //h? 的意思是,零个或者一个 h
 //? 多个前导字符不行
 //$mode = &#39;/ph?p/&#39;; //规则模式
 //.的意思是,任意一个字符,所以,两个点,就是任意两个字符
 //$mode = &#39;/p.p/&#39;; //规则模式
 //.* 表示前导任意字符,并且零个或者多个
 //$mode = &#39;/p.*p/&#39;; //规则模式
 //h{3} 表示,前导必须是3 个,
 //$mode = &#39;/ph{3}p/&#39;; //规则模式
 //h{3,5}表示,前导必须是 3 到 5 个
 //$mode = &#39;/ph{3,5}p/&#39;; //规则模式
 //h{3,} 表示,前导至少 3 个 h
 //$mode = &#39;/ph{3,}p/&#39;; //规则模式
 //$符号,一般加在模式的字符串尾巴上
 //表示从尾巴开始匹配
 //$mode = &#39;/php$/&#39;; //规则模式
 //^表示,从开头进行匹配
 //$mode = &#39;/^php/&#39;; //规则模式
 //^ 和 $ 一起用的,基本上不要用正则了,直接用 == 号即可
 //$mode = &#39;/^php$/&#39;; //规则模式
 //| 是一个条件选择符,如果有其中一个,即可匹配。
 //$mode = &#39;/php|asp/&#39;; //规则模式
 //()
 $mode = &#39;/(this) (is) (php|asp)/&#39;; //规则模式
 $string = &#39;this is php&#39;; //字符串
 //echo preg_match($mode,$string);
 if( preg_match($mode,$string)){
 echo &#39;匹配&#39;;
 }else{
 echo &#39;不匹配&#39;;
 }
?>

Demo3.php

<?php
 //元子符
 //[]中括号,是一种正则里的语法
 //里面的东西表示任意一个即可
 //[a-z] 表示 26 个字母中,随便其中一个
 //$mode = &#39;/[a-z]/&#39;; //规则模式
 //[0-9] 表示 0-9,随便其中一个
 //$mode = &#39;/[0-9]/&#39;; //规则模式
 //[asv] 其中一个
 //$mode = &#39;/[asv]/&#39;; //规则模式
 //[a-zA-Z0-9_] 表示其中一个匹配 [a-zA-Z0-9_] 中的任意一个
 //$mode = &#39;/[a-zA-Z0-9_]/&#39;; //规则模式
 //不是在开头匹配行首的^,而是中括号里的 ^
 //[^] 表示匹配除了abc 外都能匹配的字符串
 //$mode = &#39;/[^abc]/&#39;; //规则模式
 //为了让人们使用更加的方便,\w 代表 [a-zA-Z0-9_]
 //$mode = &#39;/[\w]/&#39;; //规则模式
 //\W 正好和 \w相反
 //$mode = &#39;/[\W]/&#39;; //规则模式
 //\d 和 [0-9] 一样
 //$mode = &#39;/\d/&#39;; //规则模式
 //\D 和 [^0-9] 一样
 //$mode = &#39;/\D/&#39;; //规则模式
 //\s匹配任何空白字符
 //$mode = &#39;/\s/&#39;; //规则模式
 //\S不包含空白字符
 //$mode = &#39;/\S/&#39;; //规则模式
 //\b表示到达了单词的边界,如果没有到达就不匹配,边界可以理解为空格
 //$mode = &#39;/php\b/&#39;; //规则模式
 //\B 表示没有到达边界
 //$mode = &#39;/php\B/&#39;; //规则模式
 //因为 + 号是正则里的特殊字符
 //如果要匹配特殊字符的话,必须加上 \
 $mode = &#39;/p\+p/&#39;; //规则模式
 $string = &#39;p+p&#39;; //字符串
 if( preg_match($mode,$string)){
 echo &#39;匹配&#39;;
 }else{
 echo &#39;不匹配&#39;;
 }
?>

Demo4.php

<?php
 //修饰符
 //修饰符放在 // 的外面
 //i 表示不区分大小写
// $mode = &#39;/php/i&#39;; //规则模式
// $string = &#39;PHP&#39;; //字符串
 //m 表示匹配首尾的时候,如果遇到换行,也应该承认是结尾
// $mode = &#39;/php$/m&#39;; //规则模式
// $string = "this is a php\n ,is goods"; //字符串
 //x 表示忽略掉规则模式中的空白字符
// $mode = &#39;/php/x&#39;; //规则模式
// $string = &#39;ph p&#39;; //字符串
 //A 表示必须从头开始匹配
 $mode = &#39;/php/A&#39;; //规则模式
 $string = &#39;phpfsdfd&#39;; //字符串
 if( preg_match($mode,$string)){
 echo &#39;匹配&#39;;
 }else{
 echo &#39;不匹配&#39;;
 }
?>

Demo5.php

<?php
 //搜索数组中的相匹配的字符串
 //preg_grep() 函数
 $language = array(&#39;php&#39;,&#39;asp&#39;,&#39;jsp&#39;,&#39;python&#39;,&#39;ruby&#39;);
 //找出最流行的 3p 语言
 //这三门语言的匹配共同点是什么,最后结尾都是 p
 //结尾匹配 p /p$/
 //$mode = &#39;/p$/&#39;;
 //打印出开头为 p 的语言
 $mode = &#39;/^p/&#39;;
 //python 蟒蛇语言,在国外有取代 PHP 的传闻
 print_r(preg_grep($mode,$language));
?>

Demo6.php

<?php
 //搜索模式,最后返回的是真或者是假,1,10
 echo preg_match(&#39;/php[1-6]/&#39;,&#39;php5&#39;);
?>

Demo7.php

<?php
 //电子邮件的小案例
 //通过拆分的方法分组
 $mode = &#39;/^([\w\.]{2,255})@([\w\-]{1,255}).([a-z]{2,4})$/&#39;;//模式不能缺少
 $string = &#39;oneStopWeb@163.com&#39;;
 if(preg_match($mode,$string)){
 echo &#39;电子邮件合法&#39;;
 }else{
 echo &#39;电子邮件不合法&#39;;
 }
?>

Demo8.php

<?php
 //匹配全局正则
 //将字符串的所有匹配得到的结果放到一个数组变量里
 preg_match_all(&#39;/php[1-5]/&#39;,&#39;php5fsdfsdphp6fsdfsdfphp4&#39;,$out);
 print_r($out);//Array ( [0] => Array ( [0] => php5 [1] => php4 ) )
 echo $out[0][0];//php5
?>

Demo9.php

<?php
 //定界定则
 echo preg_quote(&#39;PHP is $150&#39;); //PHP is \$150
?>

Demo10.php

<?php
 //搜索匹配的结果,然后替换掉
 //第一个参数,存的是正则模式
 //第二参数,放的是替换掉的字符串
 //第二个参数,字符
 //将第三个参数的字符串的 php5,php6 替换成了 oneStopWeb
 echo preg_replace(&#39;/php[1-6]/&#39;,&#39;oneStopWeb&#39;,&#39;This is a php5,This is a php6&#39;);
 //This is a oneStopWeb,This is a oneStopWeb
?>

Demo11.php

<?php
 //贪婪和分组获取的案例,ubb
 //我要将这个 [b][/b] 换成 <strong></strong>
 //注意一个问题,这个时候的 [] 中括号,是字符串的括号,而不是语法[a-z]
 //. 表示匹配任意字符一个,加上一个 * 号表示匹配零个或者多个
 //用括号分为三组,那么第一组就是\1,第二组就是\2,第三组就是\3
 //目前只有1 组,\1
 //第一问题,第一个[b]和最后一个[/b]匹配了
 //解决贪婪匹配。
 $mode = &#39;/(\[b\])(.*)(\[\/b\])/U&#39;;//U 禁止贪婪
 $replace = &#39;<strong>\2</strong>&#39;;
 $string = &#39;This is a [b]php5[/b],This is a [b]php6 [/b]&#39;;
 //echo $string;
 echo preg_replace($mode,$replace,$string);
?>

Demo12.php

<?php
 //用正则表达来进行分割
 //如果没有 [] 符号,就表示,要同时满足
 print_r(preg_split(&#39;/[.@]/&#39;,&#39;oneStopWeb@163.com&#39;));
 //Array ( [0] => oneStopWeb [1] => 163 [2] => com )
?>

希望本文所述对大家PHP程序设计有所帮助。

更多PHP入门教程之正则表达式基本用法实例详解相关文章请关注PHP中文网!

Statement
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
How do you set the session cookie parameters in PHP?How do you set the session cookie parameters in PHP?Apr 22, 2025 pm 05:33 PM

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

What is the main purpose of using sessions in PHP?What is the main purpose of using sessions in PHP?Apr 22, 2025 pm 05:25 PM

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How can you share sessions across subdomains?How can you share sessions across subdomains?Apr 22, 2025 pm 05:21 PM

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

How does using HTTPS affect session security?How does using HTTPS affect session security?Apr 22, 2025 pm 05:13 PM

HTTPS significantly improves the security of sessions by encrypting data transmission, preventing man-in-the-middle attacks and providing authentication. 1) Encrypted data transmission: HTTPS uses SSL/TLS protocol to encrypt data to ensure that the data is not stolen or tampered during transmission. 2) Prevent man-in-the-middle attacks: Through the SSL/TLS handshake process, the client verifies the server certificate to ensure the connection legitimacy. 3) Provide authentication: HTTPS ensures that the connection is a legitimate server and protects data integrity and confidentiality.

The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.