Home > Backend Development > PHP Tutorial > php preg_match regular expression function example, phppregmatch function_PHP tutorial

php preg_match regular expression function example, phppregmatch function_PHP tutorial

WBOY
Release: 2016-07-12 08:55:55
Original
936 people have browsed it

php preg_match regular expression function example, phppregmatch function

Regular expressions are used in almost all programming languages. This example introduces the application of the regular expression preg_match function in php .

The preg_match() function is used for regular expression matching and returns 1 successfully, otherwise it returns 0.
preg_match() will stop matching after one successful match. If you want to match all results, you need to use the preg_match_all() function.

Grammar:

preg_match (pattern , subject, matches)

参数 描述
pattern 正则表达式
subject 需要匹配检索的对象
matches 可选,存储匹配结果的数组
Parameters


Description
pattern Regular expression
subject Need to match the retrieved object
matches Optional, array to store matching results

Example:
<?<span>php
</span><span>$str</span>="Daniel J. Gross Catholic High School A. is a faith and family based community committed to developing Christian leaders through educational excellence in the Marianist tradition."<span>;
</span><span>if</span>(<span>preg_match</span>("/[A-Z]. /",<span>$str</span>,<span>$matches</span><span>)){
    </span><span>print_r</span>(<span>$matches</span><span>);
}
</span>?>
Copy after login
This example matches strings with capital letters followed by . and spaces. It can only match J., because preg_match() will stop matching after one successful match, and will not match again later. Matched.

<span>Array</span> ( [0] => J. ) 
Copy after login

Output result:

Original address: http://www.manongjc.com/article/52.html

Related reading:

PHP regular expression function usage examples

php preg_match function and php preg_match_al function l instances, methods, examples

php preg_match_all() function usage example

php preg_match regular expression function example

http://www.bkjia.com/PHPjc/1115243.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1115243.htmlTechArticle
php preg_match regular expression function example, phppregmatch function regular expressions are used in almost all programming languages, this book An example introduces the application of the regular expression preg_match function in php...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template