查找PHP进入总页码 [英] Find total page numbers entered in php

查看:130
本文介绍了查找PHP进入总页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了标记为页码

用户可以像任意顺序输入页码1,3,6

我要处理,如果用户输入 1,4,2,6-8,10 。然后,我应该知道用户选择页码 1,2,4,6,7,8,10

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.

的页数顺序号也可以改变。对于如 5,6,4-8,1 。这些数字可以重复,但我只需要独特的页码。

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.

我怎么能做到这一点在PHP?先谢谢了。

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

推荐答案

下面的进攻计划:


  1. 用逗号分割字符串,并通过迭代值

  2. 对于每一个值,验证它是一个数字或由连字符隔开的两个数字

  3. 如果它是一个单号,将其添加到您的设置页号

  4. 如果它是那么一个范围内连字符分割字符串

  5. 确定哪个号码是较小的一个,并用于从较小到较大的一个循环,每次加的数量所设定的

  6. 排序集。

我会做的一集数组assoc命令,当我想补充一个数字,我想将其设置为关键。例如。

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;

这里的code:

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);

这篇关于查找PHP进入总页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆