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 405385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:23:43+00:00 2026-05-12T17:23:43+00:00

I need to print all matches using preg_match_all. $search = preg_match_all($pattern, $string, $matches); foreach

  • 0

I need to print all matches using preg_match_all.

$search = preg_match_all($pattern, $string, $matches);

foreach ($matches as $match) {
    echo $match[0];
    echo $match[1];
    echo $match[...];
}

The problem is I don’t know how many matches there in my string, and even if I knew and if it was 1000 that would be pretty dumb to type all those $match[]‘s.

  • 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-12T17:23:44+00:00Added an answer on May 12, 2026 at 5:23 pm

    The $match[0], $match[1], etc., items are not the individual matches, they’re the “captures”.

    Regardless of how many matches there are, the number of entries in $matches is constant, because it’s based on what you’re searching for, not the results. There’s always at least one entry, plus one more for each pair of capturing parentheses in the search pattern.

    For example, if you do:

    $matches = array();
    $search = preg_match_all("/\D+(\d+)/", "a1b12c123", $matches);
    print_r($matches);
    

    Matches will have only two items, even though three matches were found. $matches[0] will be an array containing “a1”, “b12” and “c123” (the entire match for each item) and $matches[1] will contain only the first capture for each item, i.e., “1”, “12” and “123”.

    I think what you want is something more like:

    foreach ($matches[1] as $match) {
        echo $match;
    }
    

    Which will print out the first capture expression from each matched string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to print some pages with CSS backgrounds but they don't appear in
I need to print out data into a pre-printed A6 form (1/4 the size
I have a DateTime object that I need to print in a custom gridlike
I need to write a unit test for a method that will print a
I have a regex I need to match against a path like so: C:\Documents
I would like to be able to search a string for various words, when
I need to print out a 3 by 3 matrix in C. I could
I need to develop a file indexing application in python and wanted to know
I am trying to load a html page through UIWebview.I need to disable all
Hi all I'm struggling to think of a way to describe my problem so

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.