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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:35:37+00:00 2026-06-09T15:35:37+00:00

I made a textbox labeled as Page Numbers . The user may enter the

  • 0

I made a textbox labeled as “Page Numbers“.

The user may enter the page numbers in any sequence like 1, 3, 6.

I want to handle that if user entered 1, 4, 2, 6-8, 10. Then I should know that the user selected page number 1, 2, 4, 6, 7, 8, 10.

That means the user can also enter the range as well as comma separated numbers like we give the page numbers while printing documents.

The order number of pages can also be changed. For e.g. 5, 6, 4-8, 1. The numbers can be repeated but I need only the unique page numbers.

How I can do it in PHP? Thanks in Advance.

  • 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-09T15:35:39+00:00Added an answer on June 9, 2026 at 3:35 pm

    Here’s the plan of attack:

    1. Split the string by comma and iterate through the values
    2. For each value validate that it’s either a number or two numbers separated by a hyphen
    3. If it’s a single number, add it to your page number set
    4. If it’s a range then split the string by hyphen
    5. Determine which number is the smaller one and for loop from the smaller one to the larger one, each time adding the number to the set
    6. Sort the set.

    I’d make the set an assoc array and when I add a number to it I’d set it as the key. E.g.

    $pageNumbers[$number] = true;
    

    Here’s the code:

    $pageNumberStr = $_REQUEST['pageNumberStr'];
    
    $components = explode(",", $pageNumberStr);
    
    $pageNumbers = array();
    
    foreach ($components as $component) {
       $component = trim($component);
       if (preg_match('/^\d+$/', $component)) {
          $pageNumbers[$component] = true;
       } else if (preg_match('/^(\d+)-(\d+)$/', $component, $matches)) {
          $first = min($matches[1], $matches[2]);
          $last = max($matches[1], $matches[2]);
    
          for ($i = $first; $i <= $last; $i++) {
             $pageNumbers[$i] = true;
          }
       }
    }
    
    $pageNumbers = array_keys($pageNumbers);
    sort($pageNumbers);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a Cocoa program that lets the user enter their name in NSTextField.
I made an App on WP7 that uses the textbox for the user to
I want to clear the textbox having Jquery Autocomplete once the user has made
I made a custom control that is basically a multiline TextBox that allows input,
I made a user interface in VBA with many textbox. I read an excel
I've made a binding from a TextBox to a Property. If the user write
Possible Duplicate: Bind textbox to 'enter' key I've made a chat system with AJAX
In Interface Builder I made a checkbox and a textbox. When the user checks
I have made a custom textbox that inherits from textbox. using System.Drawing; using System;
I want add to my Windows phone app masked textbox, are there ready-made control

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.