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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:28:05+00:00 2026-05-26T07:28:05+00:00

I tried searching for this both here and on Google, so if I missed

  • 0

I tried searching for this both here and on Google, so if I missed something obvious, I apologize. It’s possible I simply don’t know the name for the formatting of these numbers.

What I’m looking to do is to start with a string, like “1-3,5,7-9” and have it turn into a PHP array with the following entries: 1,2,3,5,7,8,9

I know how to do this by using preg_split on a comma, then iterating through and expanding any – marks, but I feel like there must be an easier/better way.

EDIT

I didn’t make this clear, but the string needs to include SPANS! That means if my string was “1-9” my resulting array should be “1,2,3,4,5,6,7,8,9” NOT JUST “1,9”. Sorry for that not being clear before.

  • 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-26T07:28:06+00:00Added an answer on May 26, 2026 at 7:28 am

    Not entirely sure what you mean by “expanding”. Anyway, here’s how I would do it with explode and range:

    $input = '1-3,5,7-9';
    $output = array();
    
    foreach (explode(',', $input) as $nums) {
        if (strpos($nums, '-') !== false) {
            list($from, $to) = explode('-', $nums);
            $output = array_merge($output, range($from, $to));
        } else {
            $output[] = $nums;
        }
    }
    

    If there’s a better way that doesn’t use eval (or PCRE e modifier), I don’t know any.

    Here is, for your amusement, a one-liner (that unfortunately uses eval) that returns the same result, but…

    Disclaimer: Using eval is not recommended in most cases because it can create security risks and other issues. I wouldn’t use it but it’s still feasible.

    With that said, here it is:

    $output = explode(',', preg_replace('/([0-9]*)-([0-9]*)/e', 'implode(",", range($1, $2));', $input));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried searching for information on this, but must not know the right terms
I tried searching for this but couldn't find a similar question. I have a
I've tried searching for this but it's pretty difficult to put into words. Basically,
Ok, I have tried searching around for this answer, but with no luck. I
I have tried searching over the internet about this problem but not able to
I've tried searching for an answer to this problem, but I'm not entirely sure
Sorry if this question is answered somewhere else but I tried searching several pages
This is quite possibly a very stupid question but I've tried searching for and
I've tried searching through search engines,MSDN,etc. but can't anything. Sorry if this has been
I have tried to find the answer to this question on both the Spring

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.