Skip to content

Instantly share code, notes, and snippets.

@malles
Last active January 4, 2016 00:29
Show Gist options
  • Save malles/8542214 to your computer and use it in GitHub Desktop.
Save malles/8542214 to your computer and use it in GitHub Desktop.
Bewerking adres en genereren willekeurige code.
<?php
//read and convert CSV data
$file = 'Postmailing.csv';
$dataRows = array();
$del = ';';
echo '<pre>';
$csv = implode(';',adres::$csvFields)."\n";
if (($handle = fopen($file, "r")) !== FALSE) {
$i=0;
while ( ($data = fgetcsv($handle, 10000, $del) ) !== FALSE ) {
if ($i==0) {
$i++;
continue; //headers
}
$adres = new adres($data);
$csv .= $adres->csv();
$dataRows[] = $adres;
$i++;
}
fclose($handle);
}
echo file_put_contents('output.csv',$csv).' bytes'."\n";
print($csv);
echo '</pre>';
class adres {
public $code;
public $achternaam;
public $initialen;
public $straatnaam;
public $huisnummer;
public $postcode;
public $plaats;
public $telefoon;
public $codePrefix = 'A';
public static $csvFields = array('code','achternaam','initialen','straatnaam','huisnummer','postcode','plaats','telefoon');
public static $usedCodes = array();
public function __construct($row) {
$this->achternaam = $row[1];
$this->initialen = $row[2];
$this->straatnaam = $row[3];
$this->postcode = $row[4];
$this->plaats = $row[5];
$this->telefoon = $row[0];
$this->process();
}
protected function process() {
$code = '';
$this->code = $this->codePrefix.$this->generateCode('numeric-7-mix');
$regEx = '/^(?P<straat>[a-zA-Z\'\s\.\-]{0,100})\s(?P<nr>.*)/';
if (preg_match($regEx,$this->straatnaam,$match)) {
$this->straatnaam = $match['straat'];
$this->huisnummer = $match['nr'];
}
}
public function csv() {
$d = ";";
$values = array();
foreach (self::$csvFields as $csvField)
$values[] = $this->$csvField;
return implode($d,$values)."\n";
}
protected function generateCode($format) {
list($content,$iLength,$case) = explode('-',$format);
//pr($content.$iLength.$case);
$aCharacters = array();
switch ($content) {
case 'alphanumeric':
if ($case == 'cap') {
$aCharacters = array('1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z');
} elseif ($case == 'low') {
$aCharacters = array('a','b','c','d','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9');
} else {
$aCharacters = array('1','2','3','4','5','6','7','8','9','a','b','c','d','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9');
}
break;
case 'numeric':
$aCharacters = array('0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9');
break;
case 'alpha':
if ($case == 'cap') {
$aCharacters = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
} elseif ($case == 'low') {
$aCharacters = array('a','b','c','d','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
} else {
$aCharacters = array('a','b','c','d','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
}
break;
}
$code = $this->_generateCode($aCharacters,$iLength);
return $code;
}
private function _generateCode($aCharacters,$iLength=8) {
$sResult = '';
for($i = 0; $i < $iLength; $i++) {
$sResult .= $aCharacters[rand(0, sizeof($aCharacters) - 1)];
}
if (isset(self::$usedCodes[$sResult])) {
echo "Dubbel! $sResult\n";
return self::_generateCode($aCharacters,$iLength);
}
self::$usedCodes[$sResult] = true;
return $sResult;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment