Php 我如何随机地将URL的各个部分混在一起,并用“"/&引用;?

Php 我如何随机地将URL的各个部分混在一起,并用“"/&引用;?,php,url,random,shuffle,Php,Url,Random,Shuffle,我想知道如何将此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_produ

我想知道如何将此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/
................
................
................

我如何才能做到这一点?

这应该让您开始:

$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()。为了让你开始,这里有一个问题。将其更改为使用阵列应该不会太困难。

我不确定这是否真的是您想要的(根据您的评论),但要回答未来访问者的问题:

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

我很难想象你为什么想要这个。如果它是为了获得某种SEO效果,我会反对它。你可能有一个所谓的好问题Nanne!我需要得到一个较短版本的网址,例如有一个关于看,如果你打开这个网址,它将重定向到网页报废我需要这个简短的网址。希望我能把你说清楚。@Vind你在评论中要求的与你在问题中要求的完全不同。你可以考虑删除你的问题,问一个新问题吗?你的问题应该是如何为我的content@AndreschSerj我不这样认为,正如我在上面的评论中提到的,我需要找到一个简短的url,现在我正在手动执行此操作。如果我将其自动化,我可以使用它来确定它是否可用。关于我的方法还有什么问题吗?@Vind:你在问题中描述的是一个非常独特的请求,即获取长url的所有可能的混乱版本。你在评论中提到的搜索是一个希望是唯一的短url。后者可以使用一行程序
substr(md5(url),0,8)
完成,并检查是否存在冲突以防万一。尽管如此,我还是费了好大劲才回答了你最初的问题。这是有效的:对不起,这不是我要的。错了。这正是你想要的:我在寻找所有结果的组合,比如。。。。可能不是你想要的,但仍然是你要求的。我试图在评论中澄清这一点,但你不同意,并表示这是你想要的:给出了所有可能的混乱url组合。也花了一些时间来写,写起来也很有趣。所以不管怎样-p这看起来比我的答案更好,但它确实给出了一些无效的输出,例如重复同一节六次。我认为可能性的数量是n!,不是n^n,如果我读对了问题的话。你用n个数字和基数n组合的可能性是n^n,不是吗?在测试中锻炼。我当然不是数学天才,你:-7哪一部分被意外地重复了6次?修复了我上次编辑BTWWell中的一些错误,最后的输出是
fujifilm\u x\u t1/fujifilm\u x\u t1/fujifilm\u x\u t1/fujifilm\u x\u t1/fujifilm\u x\u t1/
。这不完全是我所需要的,但我从你的回答中得到了一条线索。