PHP preg match Regular Expression Function operation instance, pregmatch

Source: Internet
Author: User
Tags php preg match preg

PHP preg match Regular Expression Function operation instance, pregmatch

In php, The preg_match () function is a common function used to execute regular expressions. 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. If the regular expression matches successfully, 1 is returned. Otherwise, 0 is returned.

After a successful preg_match () match, the match is stopped. to match all the results, use the preg_match_all () function.

Syntax:

preg_match (pattern , subject, matches)

Parameters Description
Pattern Regular Expression
Subject Objects to be retrieved
Matches (Optional) array of matching results

Instance:

This instance matches strings with. And spaces after uppercase letters, and can only match J., because after a successful preg_match () match, the match will be stopped and will not be matched later.

<?php$str="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.";if(preg_match("/[A-Z]. /",$str,$matches)){print_r($matches);}?>

Output result:

Array ([0] => J .)

The following describes the length of the preg_match string.

Preg_match: the regular expression extracts the target content. If there is a problem with whether the content is live or not, the code is dead.

Later, I suspect that PHP's preg_match has a string length limit, and found that the value of "pcre. backtrack_limit" is set to only 100000 by default.

Solution:

ini_set('pcre.backtrack_limit', 999999999);

Note: this parameter is available after php 5.2.0.

For more information, see pcre. recursion_limit.

Pcre. recursion_limit is the recursive limitation of PCRE. If this item is set to a large value, it will consume the available stacks of all processes and eventually cause PHP to crash.

You can also modify the configuration to limit the following:

Ini_set ('pcre. recursion_limit ', 99999 );

In actual project applications, it is best to set the memory limit: ini_set ('memory _ limit ', '64m');, which makes it safer.

Articles you may be interested in:
  • Basic usage of the PHP function preg_match_all Regular Expression
  • Regular Expression in PHP (preg_match, preg_match_all, preg_replace, preg_split)
  • What does preg_match isU mean in php?
  • The string length of Regular Expression matching in the preg_match function in PHP
  • The meanings of/u,/I, And/s in preg_match regular match in PHP
  • Solutions to apache crash caused by PHP preg_match_all in win2003
  • PHP preg_match matching string length Problem Solution
  • PHP preg_match
  • Small php experience: parsing functions preg_match and preg_match_all
  • Use preg_match_all in php to match the image in the article
  • Php preg_match_all replace all img content with str_replace

Related Article

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.