我想知道如何将此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()
函数以给出所有可能的输出,然后将echo
放入foreach
循环中应该是相当简单的。为了让你们开始,这里有一个问题关于获得字符串的所有可能排列。修改它来处理数组应该不会太难。
我不确定它是否实际上是你想要的(由于你的评论),但为了回答未来访问者的问题:
<?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);
查看此处运行的代码(可能由于ideone内存限制而无法工作)。
如果你看到你的混叠部分作为数字,你可以很容易地计算出有多少种可能性。如果你有10个部分,你的数字系统保持不变,你有一个10位数长的数字代表你的可能性:9,999.999.999 + 1种可能性,对吗?
如果你有更少的,比如在你的例子中有6个部分,你有6^6种可能性(46656)。
Read up on…
- 基数
- 组合
- base_convert
- substr
- stripos函数
- ,
…来了解它是如何工作的。
警告:由于PHP base_convert方法的限制,它仅在不超过36个斜杠(可混叠部分)时才有效。