Home  >  Article  >  Backend Development  >  PHP preg_match regular expression function example

PHP preg_match regular expression function example

WBOY
WBOYOriginal
2016-07-29 08:55:251231browse

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, returning 1 successfully, otherwise returning 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.

Syntax:

preg_match (pattern, subject, matches)
Parameters Description
pattern regular expression
subject needs to match the search Object
matches Optional, an array that stores matching results


Instance:
This instance matches strings with capital letters followed by . and spaces, and can only match J., because preg_match( ) will stop matching after one successful match, and will not match again.

<?<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>?>

Output result:

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

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

Related reading:

php regular expression function usage example

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

php preg_match_all() function usage examples

php preg_match regular expression function examples

The above introduces the php preg_match regular expression function example, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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