The example explains how to use the PHP function preg_grep ().

Source: Internet
Author: User

The PHP function preg_grep () We introduced to you today is one of the many powerful functions in the PHP language. I hope you will have a deep understanding of this function after reading it.

Function prototype: array preg_grep (string $ pattern, array $ input)
The PHP function preg_grep () returns an array containing the units in the $ input array that match the given $ pattern. For each element in the input array $ input, preg_grep () only matches once. The example given in code 6.3 illustrates the use of the preg_grep () function.

PHP function preg_grep () code 6.3 array query matching

 
 
  1. <? Php
  2. $ Subjects = array (
  3. "Mechanical Engineering", "Medicine ",
  4. "Social Science", "Agriculture ",
  5. "Commercial Science", "Politics"
  6. );
  7. // Match the names of all subjects that contain only one word
  8. $ Alonewords = preg_grep ("/^ [a-z] * $/I"
    , $ Subjects );
  9. ?>

Through the code example of the above PHP function preg_grep (), we hope to serve readers a better impression.


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.