Skip to content

Instantly share code, notes, and snippets.

@arth2o
Created December 10, 2013 10:57
Show Gist options
  • Save arth2o/7888896 to your computer and use it in GitHub Desktop.
Save arth2o/7888896 to your computer and use it in GitHub Desktop.
Generate Faker User Data and save CSV format. Used external class: https://github.com/fzaninotto/Faker Class Use in Command line: php faker_csv.php -- 'count=10000' > lorem-10000.csv
#!/usr/bin/php
<?php
error_reporting(0);
//https://github.com/fzaninotto/Faker
require_once 'faker/autoload.php';
$faker = Faker\Factory::create('it_IT'); // create a Italy
//CSV TITLE LINE
$headers = array('unique_client_pk', 'sesso', 'nome', 'cognome', 'email', 'citta', 'cap', 'provincia', 'anno di nascita', 'registrazione', 'partner_pk', );
//php faker_csv.php -- 'count=10000' > lorem-10000.csv
parse_str($argv[2]);
$defaultCount = 100;
$count = (int)$count;
$rndDataNum = ($count>0) ? $count:$defaultCount;
$output = "";
$csv_data = array();
$quotation_marks = '"%s"';
foreach ($headers as $header) {
$csv_data[0][] = sprintf($quotation_marks, $header);
}
$output .= (implode(",", $csv_data[0])) . "\r\n";
for ($i = 1; $i < $rndDataNum; $i++) {
$male_or_female = rand(0, 1);
//unique_client_pk
$csv_data[$i][] = sprintf($quotation_marks, $faker -> randomNumber(1, 1000000));
//sesso / Male | Female
$csv_data[$i][] = sprintf($quotation_marks, (($male_or_female == 1) ? "M" : "F"));
//nome, cognome / firstname - lastname
$csv_data[$i][] = sprintf($quotation_marks, $faker -> lastName);
$csv_data[$i][] = sprintf($quotation_marks, $faker -> firstName);
//email
$csv_data[$i][] = sprintf($quotation_marks, $faker -> email);
//citta / city
$csv_data[$i][] = sprintf($quotation_marks, $faker -> city);
//cap / postcode
$csv_data[$i][] = sprintf($quotation_marks, $faker -> postcode);
//provincia / COUNTRY CODE
$csv_data[$i][] = sprintf($quotation_marks, $faker -> stateAbbr);
//anno di nascita / Birth date
$csv_data[$i][] = sprintf($quotation_marks, $faker -> dateTimeThisCentury -> format('d/m/Y'));
//registrazione / Registration date
$csv_data[$i][] = sprintf($quotation_marks, $faker -> dateTimeThisDecade -> format("d/m/Y H:i"));
//partner_pk / RND number
$csv_data[$i][] = sprintf($quotation_marks, $faker -> randomNumber(1,9999));
$output .= (implode(",", $csv_data[$i])) . "\r\n";
}
echo $output;
//print_r($csv_data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment