Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6978639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:46:58+00:00 2026-05-27T17:46:58+00:00

How do I search in an array with preg_match? Example: <?php if( preg_match( ‘/(my\n+string\n+)/i’

  • 0

How do I search in an array with preg_match?

Example:

<?php
if( preg_match( '/(my\n+string\n+)/i' , array( 'file' , 'my string  => name', 'this') , $match) )
{
    //Excelent!!
    $items[] = $match[1];
} else {
    //Ups! not found!
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:46:59+00:00Added an answer on May 27, 2026 at 5:46 pm

    In this post I’ll provide you with three different methods of doing what you ask for. I actually recommend using the last snippet, since it’s easiest to comprehend as well as being quite neat in code.

    ##How do I see what elements in an array that matches my regular expression?

    There is a function dedicated for just this purpose, preg_grep. It will take a regular expression as first parameter, and an array as the second.

    See the below example:

    $haystack = array (
      'say hello',
      'hello stackoverflow',
      'hello world',
      'foo bar bas'
    );
    
    $matches  = preg_grep ('/^hello (\w+)/i', $haystack);
    
    print_r ($matches);
    

    output

    Array
    (
        [1] => hello stackoverflow
        [2] => hello world
    )
    

    ###Documentation

    • PHP: preg_grep – Manual

    ##But I just want to get the value of the specified groups. How?

    array_reduce with preg_match can solve this issue in clean manner; see the snippet below.

    $haystack = array (
      'say hello',
      'hello stackoverflow',
      'hello world',
      'foo bar bas'
    );
    
    function _matcher ($m, $str) {
      if (preg_match ('/^hello (\w+)/i', $str, $matches))
        $m[] = $matches[1];
    
      return $m;
    }
    
    // N O T E :
    // ------------------------------------------------------------------------------
    // you could specify '_matcher' as an anonymous function directly to
    // array_reduce though that kind of decreases readability and is therefore
    // not recommended, but it is possible.
    
    $matches = array_reduce ($haystack, '_matcher', array ());
    
    print_r ($matches);
    

    output

    Array
    (
        [0] => stackoverflow
        [1] => world
    )
    

    Documentation

    • PHP: array_reduce – Manual
    • PHP: preg_match – Manual

    ###Using array_reduce seems tedious, isn’t there another way?

    Yes, and this one is actually cleaner though it doesn’t involve using any pre-existing array_* or preg_* function.

    Wrap it in a function if you are going to use this method more than once.

    $matches = array ();
    
    foreach ($haystack as $str) 
      if (preg_match ('/^hello (\w+)/i', $str, $m))
        $matches[] = $m[1];
    

    Documentation

    • PHP: preg_match – Manual
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to be able to use php search an array (or better yet,
I search a pagination script/class/helper in PHP for an Array of data, not for
I need to search a string in the string array. I dont want to
I'm using PHP's preg_match_all() to search a string imported using file_get_contents(). The regex returns
I created a search facility with ExtJS and PHP. The PHP file is the
i need to parse a search string for keywords and phrases in php, for
is there a similar function in python that takes search(array) and replace(array) as a
@search_results = Array.new duplicates = Set.new results.each { |result| @search_results.push(result) unless duplicates.add?(result[:url]) } This
Possible Duplicate: byte[] array pattern search I am trying to write a simple compression
Why people usually do binary search instead of triple search (divide the array into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.