Question

I’m wondering how to jumble sections of this URL as / as the separator:

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

I’m looking for all combinations of results, like

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/
................
................
................

How can I do this?

Was it helpful?

Solution

This should get you started:

$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;

The above outputs only one random permutation of the URL path. Tweaking the shuffle() function to give all possible outputs, and then putting the echo in a foreach loop should be reasonably straightforward. To get you started here’s a question about getting all possible permutations of a string. Changing that to work with arrays shouldn’t be too difficult.

OTHER TIPS

I am unsure if it actually is what you want (due to your comment) but to answer the question for future visitors:

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

See code running here (might not work due to ideone memory restrictions).

If you see your jumbleable parts as numbers you can easily calculate how many possibilietes there are. If you have 10 parts, your numeric system stays the same and you have a 10 digit long number representing your possibilities: 9,999.999.999 + 1 possibilities right?

If you have less, let's say like in your example 6 parts, you have 6^6 possibilities (46656).

Read up on ...

... to understand how it works.

WARNING: It only works if you do not have more than 36 slashes (jumbleable parts) due to restrictions in the PHP base_convert method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top