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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:46:54+00:00 2026-06-18T10:46:54+00:00

This is a simple problem but I’m wasting too much time trying to figure

  • 0

This is a simple problem but I’m wasting too much time trying to figure it out on my own.

I had data stored in strings with the pattern r{N}-{N}s{N} where {N} is a one or two digit number. This pattern can repeat up to 5 times. I need to extract the numbers from the pattern, preferably grouped somehow.

For example, if the string were "r4-10s5", I want to extract {4,10,5}

If the string were "r4-10s5r6-7s8" I want to extract {4,10,5} {6,7,8}

How could I get this done in PHP?

I have tried using recursive regular expressions but I cannot get them to match properly.

  • 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-06-18T10:46:56+00:00Added an answer on June 18, 2026 at 10:46 am
    $str = "r4-10s5r6-7s8";
    
    preg_match_all("#r(\d{1,2})-(\d{1,2})s(\d{1,2})#", $str, $matches);
    
    // group the matches together
    $sets = array();
    for ($i = 0, $count = count($matches[0]); $i < $count; $i++)
        $sets[] = array($matches[1][$i], $matches[2][$i], $matches[3][$i]);
    
    print_r($sets);
    

    Outputs:

    array (size=2)
      0 => 
        array (size=3)
          0 => string '4' (length=1)
          1 => string '10' (length=2)
          2 => string '5' (length=1)
      1 => 
        array (size=3)
          0 => string '6' (length=1)
          1 => string '7' (length=1)
          2 => string '8' (length=1)
    

    Another option that doesn’t have the loop would be:

    preg_match_all("#r(\d{1,2})-(\d{1,2})s(\d{1,2})#", $str, $matches, PREG_SET_ORDER);
    
    print_r($matches);
    

    Outputs:

    array (size=2)
      0 => 
        array (size=4)
          0 => string 'r4-10s5' (length=7)
          1 => string '4' (length=1)
          2 => string '10' (length=2)
          3 => string '5' (length=1)
      1 => 
        array (size=4)
          0 => string 'r6-7s8' (length=6)
          1 => string '6' (length=1)
          2 => string '7' (length=1)
          3 => string '8' (length=1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to figure out how to solve this (fairly) simple problem but I
hey guys having this really simple problem but cant seem to figure out have
I have this simple problem but I had a hard time of finding a
This must be a simple problem but i can't figure out why it's not
This could be a simple problem but is messing my head out. The thing
This appears to be a simple problem but I haven't had any luck searching
This is probably a very simple problem but I'm trying to create an array
This seems like a really simple problem but I can't seem to figure it
This seems like a simple problem but nothing nothing is fixing it. I'm trying
this may seem like a simple problem but I couldn't find it in the

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.