【问题标题】:How do I randomly jumble sections of a URL seperated by "/"?如何随机混淆以“/”分隔的 URL 部分?
【发布时间】:2014-03-21 10:04:45
【问题描述】:

我想知道如何将这个 URL 的各个部分混杂成/ 作为分隔符:

http://fujifilm.in/en/products/consumer_products/digital_cameras/x/fujifilm_x_t1/

我正在寻找所有结果组合,例如

http://fujifilm.in/en/products/consumer_products/digital_cameras/x/fujifilm_x_t1/
http://fujifilm.in/en/products/consumer_products/digital_cameras/x
http://fujifilm.in/en/products/consumer_products/digital_cameras
http://fujifilm.in/en/products/consumer_products
http://fujifilm.in/en/products
http://fujifilm.in/en/
http://fujifilm.in/
http://fujifilm.in/en/fujifilm_x_t1/
http://fujifilm.in/en/products/fujifilm_x_t1/
http://fujifilm.in/en/products/consumer_products/fujifilm_x_t1/
http://fujifilm.in/en/products/consumer_products/digital_cameras/fujifilm_x_t1/
................
................
................

我该怎么做?

【问题讨论】:

  • 我很难想象你为什么想要这个。如果是为了获得某种 SEO 效果,我建议不要这样做。你可能有一个所谓的X/Y problem
  • 好问题南妮!我需要得到一个较短版本的 url,例如,如果你打开这个 url,它会重定向到 http://www.flipkart.com/moto-x-16-gb/p/itmdthjkza6eburu?pid=MOBDTH3DMB7GEZEK 上的 http://flipkart.com/item/MOBDTH3DMB7GEZEK 用于网页报废我需要这个短 url。希望我让你明白了。
  • @Vind 您在评论中要求的内容与您在问题中要求的内容完全不同。您可能会考虑删除您的问题并提出一个新问题?您的问题应该是如何为我的内容提供短重定向 URL
  • @Andresch Serj 我不这么认为,就像我在上面的评论中提到的那样,我需要找到一个短网址,现在我正在手动执行此操作。如果我自动化这个,我可以使用这个tool 来查找它是否可用。还有关于我的方法的问题吗?
  • @Vind:您在问题中描述的是一个非常独特的请求,以获取所有可能的长网址的混乱版本。如 cmets 中所述,您搜索的是一个希望唯一的短 url。后者可以用一个衬垫substr(md5(url),0,8) 来完成,并检查是否存在以防万一。尽管如此,我实际上不想通过麻烦来回答你最初的问题。它有效:D

标签: php url random shuffle


【解决方案1】:

这应该让你开始:

$uri = 'http://fujifilm.in/en/products/consumer_products/digital_cameras/x/fujifilm_x_t1/';

$parts = parse_url($uri);
$path = $parts['path'];
$sections = explode('/', $path);
foreach ($sections as $k => $v) {
    if (!$v) {
        unset($sections[$k]);
    }
}
shuffle($sections);
echo $parts['scheme'] . '://' . $parts['host'] . '/' . implode('/', $sections) . '/' . PHP_EOL;

以上仅输出 URL 路径的一种随机排列。调整shuffle() 函数以提供所有可能的输出,然后将echo 放入foreach 循环中应该相当简单。为了让您开始,这里有一个关于 getting all possible permutations of a string 的问题。将其更改为使用数组应该不会太难。

【讨论】:

  • 不完全符合我的要求,但我从你的回答中得到了线索。
【解决方案2】:

我不确定它是否真的是您想要的(由于您的评论),但为未来的访问者回答这个问题:

<?php

function jumbleUrl($url) {
  // $jumbledUrls will be our result array  
  $jumbledUrls = array();

  // first strip and backup the domain and protocol
  $protocol = substr($url,0,stripos($url,'//')+2);
  $urlRemaining = substr($url,strlen($protocol));

  $domain = substr($urlRemaining,0,stripos($urlRemaining,'/')+1);
  $urlRemaining = trim(substr($urlRemaining,strlen($domain)),'/');

  // create array of remaining url parts
  $jumbleParts = explode('/',$urlRemaining);

  /**
   * now we use our jumbleable parts as numbers in our own number system and 
   * count thru all possibilities. See Text below.
   */
  $jumblePartsCount = count($jumbleParts);
  $possibilities = pow($jumblePartsCount,$jumblePartsCount);

  for ($i = 0; $i <= $possibilities; $i++) {
    // now we have to find the combination representing our number.
    // basically we have to convet our base 10 number to our base $possibilities number
    // Luckily php has a function for that:
    $possibilityNr = base_convert ( $i , 10 , count($jumbleParts) );

    // Now we take each "digit" of our possibilites Nr and take the 
    // jumbleablePart it represents
    $jumbledUrl = '';

    /** 
     * assuming you do not want jumbled urls like example.org/peter/peter/frank we
     * prevent parts from occuring more than once in an url.
     */
    $doublesPreventer = array();
    $doublesOccured   = false;
    for ($j=0;$j < strlen($possibilityNr);$j++) {
        $digit = intval(substr($possibilityNr,$j,1));
        if(in_array($digit,$doublesPreventer)) {
          $doublesOccured = true;
          break;
        }
        else {
          $jumbledUrl .= $jumbleParts[$digit].'/';
          $doublesPreventer[] = $digit;
        }
    }

    if(!$doublesOccured) {
    // Now we have a jumbled url and store it to our array of jumbled urls
        $jumbledUrls[] = $protocol . $domain . $jumbledUrl;

    }
  }

  return $jumbledUrls;
}

$url = 'http://fujifilm.in/en/products/consumer_products/digital_cameras/x/fujifilm_x_t1/';
$jumbledUrls = jumbleUrl($url);

var_dump($jumbledUrls);

查看运行 here 的代码(由于 ideone 内存限制可能无法工作)。

如果您将杂乱无章的部分视为数字,则可以轻松计算出有多少可能性。如果您有 10 个部分,那么您的数字系统保持不变,并且您有一个 10 位长的数字代表您的可能性:9,999.999.999 + 1 个可能性,对吗?

如果你有更少,让我们说像你的例子中的 6 个部分,你有 6^6 种可能性 (46656)。

继续阅读...

...了解它的工作原理。

警告:由于 PHP base_convert 方法的限制,它仅适用于斜线不超过 36 个(混乱部分)的情况。

【讨论】:

  • 对不起,这不是我要求的。
  • 错了。这正是您所要求的:我正在寻找所有结果组合,例如 ...。可能不是你想要的,但仍然是你要求的。我试图在 cmets 中澄清这一点,但您不同意并说这就是您想要的:所有可能的乱码 url 组合。也花了一些时间来写它——而且写起来也很有趣。所以......无论如何;-p
  • 这看起来比我的答案更好,但它确实给出了一些无效的输出,例如重复相同的部分六次。如果我正确阅读问题,我认为可能性的数量是 n!,而不是 n^n。
  • 以 n 为基数的 n 位数字组合的可能性是 n^n 不是吗?在测试中发挥作用。我肯定不是数学天才:-7 哪个部分意外地被 grepeated 6 次?修复了我上次编辑中的一些错误 BTW
  • 嗯,最后的输出是fujifilm_x_t1/fujifilm_x_t1/fujifilm_x_t1/fujifilm_x_t1/fujifilm_x_t1/fujifilm_x_t1/
猜你喜欢
  • 2020-08-24
  • 1970-01-01
  • 2017-03-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多