Skip to content

Instantly share code, notes, and snippets.

@willwade
Created March 11, 2012 08:19
Show Gist options
  • Save willwade/2015560 to your computer and use it in GitHub Desktop.
Save willwade/2015560 to your computer and use it in GitHub Desktop.
LexiconToCSV - Convert a binary file to a csv of words
<?php
$error = "";
$done = false;
if($_FILES){
$source = file_get_contents($_FILES['ufile']['tmp_name']);
unlink($_FILES['ufile']['tmp_name']);
preg_match_all("|([a-zA-Z ']{2,})|i",$source,$out);
if(is_array($out[1])){
header('Content-disposition: attachment; filename=lexicon.csv');
header('Content-type: application/csv');
echo implode(",\n", $out[1]);
exit;
$done = true;
} else {
$done = false;
$error = "Sorry we couldn't extract any data in that. Are you sure it had words in? <br />";
}
//foreach ($out as $word){
//need to check word or sentence is ok
//}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Simons Amazingly Magical Lexicon Converter</title>
<meta name="generator" content="BBEdit 10.0" />
</head>
<body>
<? if ($done==false){ ?>
<b><?=$error?></b><br />
<h3>Upload your file here with your words you would like as a big list</h3>
NB: It just looks for words and word chunks (valid sentences). Its not foolproof. You might need to edit the final csv file. Enjoy!<br/>
<form action="lexicon2csv.php" method="post" enctype="multipart/form-data">
<input type="file" name="ufile" />
<input type="submit" value="Upload" />
</form>
<? } else { ?>
Done!
<? } ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment