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

  • Home
  • SEARCH
  • 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 6889367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:08:50+00:00 2026-05-27T06:08:50+00:00

I would like to know how to get multiple occurrences from a regex. $str

  • 0

I would like to know how to get multiple occurrences from a regex.

$str = "Some validations <IF TEST>firstValue</IF> in <IF OK>secondValue</IF> end of string.";
$do = preg_match("/<IF(.*)>.*<\/IF>/i", $str, $matches);

This is what I’ve done so far. It works if I have only 1 , but if I have more it doesn’t return the right values. Here is the result:

Array ( [0] => firstValue in secondValue [1] => TEST>firstValue in

I need to get the “TEST” and the “OK” values.

EDIT: I’ve brought the modifications suggested, thanks a lot it works fine ! However, I am now trying to add a elsif parameter and can’t get it to work well. Here is what I’ve done:

$do = preg_match_all("~<IF([^<>]+)>([^<>]+)(</IF>|<ELSEIF([^<>]+)>([^<>]+)</IF>)~", $str, $matches, PREG_SET_ORDER);

and the results is

Array 
( 
    [0] => Array 
           (
              [0] => firstValuesecondValue 
              [1] => TEST 
              [2] => firstValue 
              [3] => secondValue 
              [4] => TEST1 
              [5] => secondValue 
           ) 
    [1] => Array 
           ( 
               [0] => thirdValue 
               [1] => OK 
               [2] => thirdValue 
               [3] => 
           ) 
) 

Is there a way to make my array more clean ? It has many elements which are useless like the [0][4] etc.

  • 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-27T06:08:51+00:00Added an answer on May 27, 2026 at 6:08 am

    You should make the regex more specific. The .* that you are using should either be less greedy, or better yet disallow other angle brackets:

    ~<IF([^<>]+)>([^<>]+)</IF>~i
    

    More importantly, you should use preg_match_all, not just preg_match.

    preg_match_all("~<IF([^<>]+)>([^<>]+)</IF>~i", $str, $matches, PREG_SET_ORDER);
    

    That’ll give you a nested array like:

    [0] => Array
        (
            [0] => <IF TEST>firstValue</IF>
            [1] =>  TEST
            [2] => firstValue
        )
    
    [1] => Array
        (
            [0] => <IF OK>secondValue</IF>
            [1] =>  OK
            [2] => secondValue
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to get the name of the property that
I would like to know what events get executed (if any) after a user
With the XML below, I would like to know how to get the value
I'm using SQL Server 2005 and would like to know how I can get
i would like know some reference. I know i can googling it. but prefer
I would like to know how it is possible to get different scores for
I would like to know if it is possible to get 'leftover' calls to
I am sending a GET request to a site and would like to know
I would like to know how can we tell which consumer will get the
I would like to know how to implement a way to get a random

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.