dongyuan1983 2017-10-17 10:14
浏览 64
已采纳

PHP:使用递增的字符变量进行计数时的比较

I am doing an Excel-like webapp where the columns are "numbered" like a,b,c,...,aa,ab,...,az,ba,...,zz,aaa,.... But if I write

$start = 'a';
$end = 'z';

while($start <= $end){
    echo $start++ . ", ";
}

(notice $start <= $end) it will go a up to yz, not just a-z because

echo 'aa' <= 'z'; // true, but
echo 'za' <= 'z'; // false.

Is there a function or a way to compare two variables taking in mind that

$a = "z"; echo ++$a; // aa
$a = "zz"; echo ++$a; // aaa

so that

$a = "z"; $b = $a; $b++; // $b = aa

AisLessThanB(a,b); // returns true

and so on? That means where

a < z < aa < az < zz < aaa < zzz < aaaa

is always true?

EDIT: Something like

$start = 'a';
$end = 'cv'; // 100 columns

should work since we work with loads of columns.

Thank you!

  • 写回答

4条回答 默认 最新

  • dongxiao1591 2017-10-17 12:15
    关注

    Try this:

    // From https://stackoverflow.com/a/3580935/3088508
    function getLetterIndexInAlphabet ($letter) {
        return ord($letter) - 96;
    }
    
    function convertStringToNumber ($inStr) {
        $letters = array_reverse(str_split($inStr));
        $outNum = 0;
        for ($i = count($letters) - 1; $i >= 0; $i--) {
            $outNum += getLetterIndexInAlphabet($letters[$i]) * (pow(26, $i));
        }
        return $outNum;
    }
    
    function AisLessThanB ($a, $b) {
        $aInt = convertStringToNumber($a);
        $bInt = convertStringToNumber($b);
        return ($aInt < $bInt);
    }
    

    convertStringToNumber is the most complicated function in here, so let's have a look at how it works on a few values:

    • 'c' evaluates to 2 which is calculated by:
      1. 'c' = 3 (index in alphabet)
    • 'ba' evaluates to 53 which is calculated by:
      1. 'b' = 52 = (2 (index in alphabet) x (26 (base value) ^ 1 (column number))).
      2. 'a' = 1 (index in alphabet)
    • 'bca' evaluates to 1431 which is calculated by:
      1. 'b' = 1352 = (2 (index in alphabet) x (26 (base value) ^ 2 (column number))).
      2. 'c' = 78 = (3 (index in alphabet) x (26 (base value) ^ 1 (column number))).
      3. 'a' = 1 (index in alphabet)
    • 'bdca' evaluates to 37935 which is calculated by:
      1. 'b' = 35152 = (2 (index in alphabet) x (26 (base value) ^ 3 (column number))).
      2. 'd' = 2704 = (4 (index in alphabet) x (26 (base value) ^ 2 (column number))).
      3. 'c' = 78 = (3 (index in alphabet) x (26 (base value) ^ 1 (column number))).
      4. 'a' = 1 (index in alphabet)

    Here's some test cases:

    echo "   a < z    = " . ((AisLessThanB(   'a',    'z')) ? 'true' : 'false') . "
    ";
    echo "   z < aa   = " . ((AisLessThanB(   'z',   'aa')) ? 'true' : 'false') . "
    ";
    echo "  aa < az   = " . ((AisLessThanB(  'aa',   'az')) ? 'true' : 'false') . "
    ";
    echo "  az < zz   = " . ((AisLessThanB(  'az',   'zz')) ? 'true' : 'false') . "
    ";
    echo "  zz < aaa  = " . ((AisLessThanB(  'zz',  'aaa')) ? 'true' : 'false') . "
    ";
    echo " aaa < zzz  = " . ((AisLessThanB( 'aaa',  'zzz')) ? 'true' : 'false') . "
    ";
    echo " zzz < aaaa = " . ((AisLessThanB( 'zzz', 'aaaa')) ? 'true' : 'false') . "
    ";
    echo "
    ===================
    
    ";
    echo "   z < a    = " . ((AisLessThanB(   'z',    'a')) ? 'true' : 'false') . "
    ";
    echo "  aa < z    = " . ((AisLessThanB(  'aa',    'z')) ? 'true' : 'false') . "
    ";
    echo "  az < aa   = " . ((AisLessThanB(  'az',   'aa')) ? 'true' : 'false') . "
    ";
    echo "  zz < az   = " . ((AisLessThanB(  'zz',   'az')) ? 'true' : 'false') . "
    ";
    echo " aaa < zz   = " . ((AisLessThanB( 'aaa',   'zz')) ? 'true' : 'false') . "
    ";
    echo " zzz < aaa  = " . ((AisLessThanB( 'zzz',  'aaa')) ? 'true' : 'false') . "
    ";
    echo "aaaa < zzz  = " . ((AisLessThanB('aaaa',  'zzz')) ? 'true' : 'false') . "
    ";
    

    And they output:

       a < z    = true
       z < aa   = true
      aa < az   = true
      az < zz   = true
      zz < aaa  = true
     aaa < zzz  = true
     zzz < aaaa = true
    
    ===================
    
       z < a    = false
      aa < z    = false
      az < aa   = false
      zz < az   = false
     aaa < zz   = false
     zzz < aaa  = false
    aaaa < zzz  = false
    

    eval.in demo

    Thanks for asking such an interesting question!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?