PHP字符串排列

发布于 2024-12-08 11:56:09 字数 152 浏览 1 评论 0原文

假设我有一个由“a-zA-Z0-9”范围内的字符组成的数组。我想要做的是获得 4 个字母范围内所有可能的组合。字符串中的字符可以重复。我正在寻找一种暴力的方法来做到这一点。

这就是我相信迭代的方式:

“aaaa” “啊啊” “啊啊啊” ... “9999”

Let's say I have an array consisting of characters from these ranges "a-zA-Z0-9". What I want to do is get all possible combinations possible in a 4-letter range. Characters can be repeated in the string. I'm looking for a brute-force way to do it.

This is how I believe the iteration would be:

"aaaa"
"aaab"
"aaac"
...
"9999"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2024-12-15 11:56:09

以暴力方式:

$chars = array_merge(range('0', '9'), range('a', 'z'), range('A', 'Z'));
$cnt = count($chars);
$strings = array();
for ($first = 0; $first < $cnt; $first++) {
   for ($second = 0; $second < $cnt; $second++) {
       for ($third= 0; $third< $cnt; $third++) {
           for ($fourth= 0; $fourth < $cnt; $fourth++) {
              $strings[] = $chars[$first] . $chars[$second] . $chars[$third] . $chars[$fourth];
           }
       }
   }
}

您最终会得到一个大数组,因此您可能希望将一些数据库操作散布到其中一个循环中,这样 $strings 就不会变得太大并通过命中内存来杀死您的脚本限制。

In brute-force fashion:

$chars = array_merge(range('0', '9'), range('a', 'z'), range('A', 'Z'));
$cnt = count($chars);
$strings = array();
for ($first = 0; $first < $cnt; $first++) {
   for ($second = 0; $second < $cnt; $second++) {
       for ($third= 0; $third< $cnt; $third++) {
           for ($fourth= 0; $fourth < $cnt; $fourth++) {
              $strings[] = $chars[$first] . $chars[$second] . $chars[$third] . $chars[$fourth];
           }
       }
   }
}

You'll end up with a honkin' big array, so you might want to sprinkle some database operations into one of the loops, so $strings doesn't get too big and kill your script by hitting a memory limit.

摇划花蜜的午后 2024-12-15 11:56:09

要随机访问,您可以使用base_convert:

function getHash($i)
{
  //- allowed Letters
  $letters = 'aAbBcCdDeEfFgG';
  $len = strlen($letters);

  //- convert $i to base of $letters
  $i_in_base_of_letter = base_convert($i, 10, $len);

  //- replace letters
  return strtr($i_in_base_of_letter, '0123456789abcdefghijklmnopqrstuvwxyz', $letters);

}

或者如果您的基数大于36(您有超过36个字符),则

function getHash($i)
{
  //- allowed Letters
  $letters = 'aAbBcCdDeEfFgG';
  $letters_arr = str_split($letters, 1);

  $len = strlen($letters);

  $out ='';

  if ($i>0)
  {
    while ($i>0)
    {
      $r = $i % $len;
      $out = $letters_arr[$r] . $out;
      $i = floor ($i / $len);
    }
    return $out;
  }
  else
  {
    return $letters_arr[0];
  }
}

可以使用manuel进行迭代,您可以使用:

for($i=0; $i<100; $i++)
{
  echo getHash($i) ."\n";
}

这种方法不如蛮力方法那么快。

问候托马斯

to random access you can use base_convert:

function getHash($i)
{
  //- allowed Letters
  $letters = 'aAbBcCdDeEfFgG';
  $len = strlen($letters);

  //- convert $i to base of $letters
  $i_in_base_of_letter = base_convert($i, 10, $len);

  //- replace letters
  return strtr($i_in_base_of_letter, '0123456789abcdefghijklmnopqrstuvwxyz', $letters);

}

or manuel if your base is larger then 36 (you have more then 36 Chars)

function getHash($i)
{
  //- allowed Letters
  $letters = 'aAbBcCdDeEfFgG';
  $letters_arr = str_split($letters, 1);

  $len = strlen($letters);

  $out ='';

  if ($i>0)
  {
    while ($i>0)
    {
      $r = $i % $len;
      $out = $letters_arr[$r] . $out;
      $i = floor ($i / $len);
    }
    return $out;
  }
  else
  {
    return $letters_arr[0];
  }
}

to iterate you can use:

for($i=0; $i<100; $i++)
{
  echo getHash($i) ."\n";
}

this way is not that fast as the brute force methode.

Regards Thomas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文