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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:33:55+00:00 2026-05-26T01:33:55+00:00

I want to translate/hydrate/expand/parse a comma-separated string of integers and hyhenated integer range expressions

  • 0

I want to translate/hydrate/expand/parse a comma-separated string of integers and hyhenated integer range expressions and populate an array with its equivalent values as individual integers elements.

Input strings might look like the following:

3,5,6,9,11,23

or

3-20

or

3-6,8,12,14-20

I want to return these as an array of integers, so the last one would become:

[3,4,5,6,8,12,14,15,16,17,18,19,20]

Is there either a function available that does this, or how would I start in writing one?

  • 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-26T01:33:56+00:00Added an answer on May 26, 2026 at 1:33 am

    You are probably looking for the range function and implode:

    $input = '3,5,6,9,11,23,14-77';
    
    $result = preg_replace_callback('/(\d+)-(\d+)/', function($m) {
        return implode(',', range($m[1], $m[2]));
    }, $input);
    

    gives you:

    3,5,6,9,11,23,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77
    

    Demo

    How it works: You basically have two tokens in your string: the range-token (1-n) (defined as regular expression: (\d+)-(\d+)) and the fallback (anything else).

    preg_replace_callback allows the expansion of the token string by a callback function. That callback function then just expands the two matched numerical values into the comma-separated list by using PHP’s range function and implode.

    After that the string is in a normalized format you can just explode it:

    // as array:
    
    print_r(explode(',', $result));
    

    Full Demo


    And after years as it was requested well formulated, the integer array, you can easily treat it as a JSON Array:

    $result = json_decode('['. preg_replace_callback('/(\d+)-(\d+)/', function($m) {
        return implode(',', range($m[1], $m[2]));
    }, $input) .']');
    var_dump($result);
    

    Demo PHP 5.3-8.1 + Git Master

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

Sidebar

Related Questions

I want to translate all the keys from the array that occur in this
I want to write a simple Ruby DSL to translate some statements and expressions
I have a 32-bit integer value and I want to translate that into two
I want to translate app in Android (strings.xml and array.xml), but I don't know
i want to translate user entered english content to hindi language.So, for that whenever
I want to translate a paragraph containing a URL in a Django 1.3 application.
I want to translate text in Indian languages. I have gone through many articles
I want to translate following simple sql query into Linq to NHibernate: SELECT NewsId
I want to translate a frontend Magento store using only one csv file. So
I have some query.i want to translate from one language to another langugae.i am

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.