search
Homephp教程php手册PHP中的正则表达式函数preg_

PHP中的正则表达式函数preg_

Jun 13, 2016 am 10:58 AM
matchphpfunctionmatchregularofexpression

preg_match();     //用于正则表达式的匹配,且只匹配一次

 

preg_match_all();//用于正则表达式的匹配,会对所有符合规则的都进行匹配

 

preg_replace();   //正则表达式替换函数

preg_splite();     //正则分割函数

 

 

preg_match ( mode, string subject , arraymatches )

 

其中mode是正则规则,string subject是要匹配的字符串,arraymatches是匹配的结果数组

 

例子:

 

[php]  

$mode='/[89]/';//匹配8或者9  

$str="djfkdsjk10903990sjdfdk";  

preg_match($mode,$str,$arr);  

print_r($arr);  

?>  

 

输出:

[html]  

Array ( [0] => 9 )   

 

preg_match_all ( mode, string subject , arraymatches )

该函数和preg_match_all()函数的功能类似,不过该函数会匹配所有符合要求的内容,并将之存放到字符串中。

 

 

例子:

 

[php]  

$mode='/[89]/';//匹配8或者9  

$str="dj33f44k88dsjk10903990sjdfdk";  

preg_match_all($mode,$str,$arr);  

print_r($arr);  

?>  

输出:

 

[html] 

Array ( [0] => Array ( [0] => 8 [1] => 8 [2] => 9 [3] => 9 [4] => 9 ) )   

 

preg_replace ( mixed pattern, mixed replacement,mixed subject [, int limit] )

通过正则表达式来替换相关内容,类似之前学过的str_replace字符串替换,但功能要强于它。

 

特点:1、替换内容可以是一个正则也可以是数组正则

 

          2、替换内容可以通过修正符e来解决替换执行内容

 

用途:替换一些比较复杂的内容上,也可以用于内容的转换上

例子1——数组正则:

 

[php]  

$mode=array('/{title}/','/{author}/','/{url}/');  

$re=array("code cloud","qianshou","http://codecloud.duapp.com/");  

$str="标题:{title}
作者:{author}
地址:{url}";  

echo "


";  

if($tag=preg_replace($mode,$re,$str)){  

 echo $tag;  

}else{  

 echo "替换不成功!";  

}  

?>  

 

输出:

 

[html]  

标题:code cloud  

作者:qianshou  

地址:http://codecloud.duapp.com/  

 

例子2——ubb代码的替换:

[php]  

$str="欢迎来到我的博客:[url]http://blog.csdn.net/qsyzb[/url]";  

$re=preg_replace('/\[url\](.*)\[\/url\]/',"\\1",$str);  

echo "


".$re."
";  

?>  

 

输出:

[html 


欢迎来到我的博客:http://blog.csdn.net/qsyzb
 

 

preg_split ( string pattern, string subject [, intlimit [, int flags]] )

通过正则表达式来切割相关内容,类似之前学过的explode切割函数,但explode只能按照一种方式切割有局限性。

 

例子:

 

[php]  

$mode='/[,.#]/';  

$str='one,two.three#four';  

if($tag=preg_split($mode,$str)){  

 print_r($tag);  

}else{  

 echo "替换不成功!";  

}  

?>  

 

输出:

[html]  

Array  

(  

    [0] => one  

    [1] => two  

    [2] => three  

    [3] => four  

)  

 

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

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

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.