Skip to content

Instantly share code, notes, and snippets.

@ChrisFrench
Created June 6, 2014 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisFrench/72647961579cc93147a2 to your computer and use it in GitHub Desktop.
Save ChrisFrench/72647961579cc93147a2 to your computer and use it in GitHub Desktop.
URLGenerator.php
<?php
Class URLGenerator {
var $base = 'wdl.msft.cc/band/';
var $amount = '5600';
var $group = '1';
var $uuid = 'n40u';
var $filename = 'wdl';
var $min = 0;
var $list = array();
var $links = array();
//protected $chars = array('c','x','7','O','f','6','A','1','o','d','B','v','4','J','T','H','S','l','q','k','C','t','F','j','0','n','g','M','N','W','3','V','5','G','m','2','X','U','r','Q','y','u','R','p','K','b','D','w','9','s','i','a','8','L','P','z','I','Z','Y','h');
protected $chars = array('c','1','n','4','o','u','0','m','r','s','e','w','h','b','3','6','8','i','k','f','z','v','x','p','5','j','t','q','y','7','9','l','g','2','a','d');
// Instantiate mapper
public function __construct($length = 10, $subdomain = null) {
$this->uuidlen = count($this->chars);
$this->uuidlen--;
}
public function getShortUrl() {
$exploded = str_split($this->uuid);
$positions = array();
// convert our characters to array positions of the chartater array
foreach ($exploded as $value) {
$positions[] = array_search($value, $this->chars);
}
// now we need to calucation the increase, so we add +1 to the last element and check if we need to increase going left.
$keys = array_keys($positions);
$currentKey = end($keys);
$update = true;
while ($update == true) {
$positions[$currentKey]++;
if($positions[$currentKey] > $this->uuidlen) {
$positions[$currentKey] = $this->min;
$currentKey--;
} else {
$update = false;
}
}
//convert the positions back to a string
$url = '';
$url .= $this->base;
$url .= $this->group;
$tagid = '';
foreach ($positions as $key) {
$tagid .= $this->chars[$key];
}
$url .= $tagid;
$this->uuid = $tagid;
return $url;
}
function makeList() {
for ($i=0; $i < $this->amount; $i++) {
$this->links[] = $this->getShortUrl();
}
$this->links = array_unique($this->links);
foreach ($this->links as $key => $value) {
$this->list[] = array($value);
}
return $this->list;
}
}
$fp = fopen('php://output', 'w');
$class = New URLGenerator;
$list = $class->makeList();
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="'.$class->filename.'.csv"');
foreach ($list as $value) {
fputcsv($fp, $value);
}
fclose($fp);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment