2011-12-02 60 views
8

有一个字符串,其中包含字符[a-zA-Z0-9]。这应该是26 * 2 + 10 = 62个可能性在一个字符和62^2在二。增加这种字符串的值的首选方法是什么,以便'aA'变成'aB'等?有没有PHP中的内置东西,可以帮助?如何增加PHP中的字母数字字符串?

我知道你可以增加一个字符串,但这只是小写字母。本质上,结果应该以61为增量从'a'到'aa'。

+0

+1的修改,有趣的这个问题。 – Giberno

+2

我不知道PHP做了这件事,但我现在就做。 http://php.net/manual/en/language.operators.increment.php –

+0

期望的进展是什么?我的意思是,'a b c ... x y z A B C ... X Y Z 0 1 2 ... 7 8 9 aa'不是,显然。那么哪些字符变化最快,顺序如何? –

回答

2

的@simshaun不要为我工作。我检查了文档,发现可以为你工作的基地(基地35)base_convert和“francesco [at] paladinux.net”的评论与一个函数在base65上工作。

因此该解决方案可以是:

转换到B10 - >增量+1 - >转化基地65

编辑1

谈论转换,我认为为base64编码,所以我写了这2个funcs使用base64编码/解码数字。 不幸的是,所使用的字符集有点大:[a-zA-Z0-9/+ =],但使用更高效的内部函数。

零 0是 “AA ==”

function nencode($in){ 
     $res =""; 
     while(1){ 
       $b = $in & 0xff; 
       $res = chr($b) . $res; 

       if($in > 0xff){ 
         $in = $in >> 8; 
       } else { 
         break; 
       } 
     } 
     return base64_encode($res); 
} 

function ndecode($in){ 

     $rv=0; 
     $res =base64_decode($in); 
     for($t = 0 ; $t < strlen($res) ; $t++){ 
       if($t>0){ 
         $rv = $rv << 8; 
       } 
       $c = ord($res{$t}); 
       $rv |= $c; 
     } 
     return $rv; 
} 
+0

你不能'base_convert'与基地> 36 – Fluffy

+0

你有没有看到francesco [at] paladinux.net的帖子? –

+0

对不起,我不知道我怎么可能错过 – Fluffy

4

这个工作对我来说:

<?php 
$str = 'a'; 
echo ++$str; // b 

$str = 'z'; 
echo ++$str; // aa 

$str = 'aA'; 
echo ++$str; // aB 
+0

这与小写字母相同 - 大写字母将保持大写。 – Fluffy

+0

所以你正在寻找一种从'zz'到'aA'的方式?认为我误解了这个问题。 – simshaun

+1

那么,如果我们谈论1个字符串,'$ a ='a''应该以61为增量变成'aa'。你的解决方案是在25。 – Fluffy

2

试试这个功能:

<?php 
function increment(&$string){ 
    $last_char=substr($string,-1); 
    $rest=substr($string, 0, -1); 
    switch ($last_char) { 
    case '': 
     $next= 'a'; 
     break; 
    case 'z': 
     $next= 'A'; 
     break; 
    case 'Z': 
     $next= '0'; 
     break; 
    case '9': 
     increment($rest); 
     $next= 'a'; 
     break; 
    default: 
     $next= ++$last_char; 
    } 
    $string=$rest.$next; 
} 

    //sample 
    $string='a'; 
    for($i=1;$i<=128;$i++){ 
     echo $string."<br>"; 
     increment($string); 
    } 
    ?> 
+0

'a9'后面的'aaa'而不是'ba'。 –

+0

你是对的,修好了 – phobia82

+0

现在它工作正常 – phobia82

1

我认为这从根本上做你以后。

<?php 

$chars = array('', 
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 
    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); 

$increment1 = 0; 
$increment2 = 0; 

for ($i = 0; $i <= 200; $i++) { 
    if ($increment2 === 62) { 
     $increment1++; 
     $increment2 = 1; 
    } else { 
     $increment2++; 
    } 
    echo "{$chars[$increment1]}{$chars[$increment2]} "; 
} 

/* 
Output: 
a b c d e f g h i j k l m n o p q r s t u v w x y z 
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
0 1 2 3 4 5 6 7 8 9 aa ab ac ad ae af ag ah ai aj ak al 
am an ao ap aq ar as at au av aw ax ay az aA aB aC aD aE 
aF aG aH aI aJ aK aL aM aN aO aP aQ aR aS aT aU aV aW aX 
aY aZ a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 ba bb bc bd be bf bg 
bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz 
bA bB bC bD bE bF bG bH bI bJ bK bL bM bN bO bP bQ bR bS 
bT bU bV bW bX bY bZ b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ca cb 
cc cd ce cf cg ch ci cj ck cl cm cn co 
*/ 
2

我喜欢这一个。如何使用: ClassX :: increment('p04E7K',6); ClassX :: decrement('p04E7K',6);

代码:

class ClassX { 
    public static $baseCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

    public static function increment($x, $digits) 
    { 
     $charList = static::$baseCharacters; 

     // completa cadeia de caracteres com o numero de digitos parametrizados 
     $x = trim($x); 
     if(strlen($x) < $digits) { 
      $x = str_pad($x, $digits, substr($charList, 0, 1), STR_PAD_LEFT); 
     } 

     $result = preg_split("//", $x, -1, PREG_SPLIT_NO_EMPTY); 
     // percorre a cadeia de caracteres de tras pra frente e incrementa o primeiro caractere possivel 
     for ($i = $digits - 1; $i >= 0; --$i) 
     { 
      $char = $result[$i]; 
      $currentChar = strpos($charList, $char); 
      $nextCharPosition = $currentChar+1; 

      if($nextCharPosition < strlen($charList)) { 
       $nextChar = substr($charList, $nextCharPosition, 1); 

       $result[$i] = $nextChar; 
       break; 
      } 
     } 

     return implode('', $result); 
    } 

    public static function decrement($x, $digits) 
    { 
     $charList = static::$baseCharacters; 

     // completa cadeia de caracteres com o numero de digitos parametrizados 
     if(strlen($x) < $digits) { 
      $x = str_pad($x, $digits, substr($charList, 0, 1), STR_PAD_LEFT); 
     } 

     $result = preg_split("//", $x, -1, PREG_SPLIT_NO_EMPTY); 
     // percorre a cadeia de caracteres de tras pra frente e decrementa o primeiro caractere possivel 
     for ($i = $digits - 1; $i >= 0; --$i) 
     { 
      $char = $result[$i]; 
      $currentChar = strpos($charList, $char); 
      $previousCharPosition = $currentChar-1; 

      if($previousCharPosition > -1) { 
       $previousChar = substr($charList, $previousCharPosition, 1); 

       $result[$i] = $previousChar; 

       // define os caracteres apos o decrementado para o ultimo caractere. ex: 3[00] > 2[99] 
       for ($j = $i + 1; $j < $digits && $i < $digits - 1; ++$j) 
        $result[$j] = substr($charList, strlen($charList)-1, 1); 
       break; 
      } 
     } 

     return implode('', $result); 
    } 
} 
0

我来到了这个问题,我也一直在寻找一种方式来生成自动递增字母数字的唯一ID非常相似,我们在移动货币(M-PESA)在肯尼亚。 作为参考,下面是该交易Screenshot showing sample MPESA transactions 我想离开这里,如果有人也在寻找类似属性的截图:即 - 自动增量字母数字 - 以大写 所有字符 - 字符串长度的自动调高一次用尽。 - 增量正是从那时候0到9 A到Z递增数量的相邻字符之前并@ phobia82的答案

function increment($string){ 
    $last_char=substr($string,-1); 
    $rest=substr($string, 0, -1); 
    switch ($last_char) { 
     case '': 
      $next= 'A'; 
      break; 
     case 'Z': 
      $next = '0'; 
      $unique = increment($rest); 
      $rest = $unique; 
      break; 
     case '9': 
      $next= 'A'; 
      break; 
     default: 
      $next = ++$last_char; 
      break; 
    } 
    $string=$rest.$next; 
    return $string; 
} 
相关问题