PHP preg match Regular Expression function action instance _ regular expression

Source: Internet
Author: User
Tags php preg match preg regular expression

In PHP, the Preg_match () function is a common function used to execute regular expressions. Regular expressions are used in almost all programming languages, and this example introduces the application of the Preg_match function of the PHP positive expression.

The Preg_match () function is used to match a regular expression, successfully returning 1, otherwise returning 0.

The Preg_match () match will stop the match once successful, and if you want to match all the results, you need to use the Preg_match_all () function.

Grammar:

Preg_match (pattern, subject, matches)

Parameters Describe
Pattern Regular expressions
Subject Objects that need to match the retrieved
Matches Optionally, storing an array of matching results

Instance:

This instance matches a string with a. And a space followed by a capital letter that can only be matched to J., because the Preg_match () match succeeds once and then stops the match and no more matches.

<?php
$str = "Daniel J. Gross Catholic High School A. are a faith and family based community committed to developing C Hristian leaders through educational excellence in the Marianist tradition. ";
if (Preg_match ("/[a-z]. /", $STR, $matches)) {
print_r ($matches);
}
? >

Output results:

Array ([0] => J.)

Let me introduce you to Preg_match string length problem

Preg_match is to extract the target content, dead and dead have problems, the code measured to death.

Later suspected PHP Preg_match has string length limit, sure enough, found that the value of "Pcre.backtrack_limit" by default only set 100000.

Solution:

Ini_set (' Pcre.backtrack_limit ', 999999999);

Note: This parameter is available after PHP version 5.2.0.

In addition to say about: Pcre.recursion_limit

Pcre.recursion_limit is the recursive limit of the pcre, which, if set to a large value, consumes all the available stacks of the process and eventually causes PHP to crash.

You can also restrict by modifying the configuration:

Ini_set (' Pcre.recursion_limit ', 99999);

In the actual project application, it is best to limit the memory setting: Ini_set (' memory_limit ', ' 64M '); , which is more secure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.