function show_form($errors = ‘A’) {if ($_POST[‘_submit_check’]) {$defaults = $_POST;}
if ($errors) {$error_text = ‘
- ‘;$error_text .= implode(‘
- ‘, $errors);$error_text .= ‘
‘;}
else {$error_text = ”;}
include ‘form.html’;}
notextile. function validate_form() {$errors = array();
if (strlen($_POST[‘textsource’]) == 0) {
$errors[] = ‘Please enter some text for character counting!’;
}
if (strlen($_POST[‘textsource’]) > 64000) {
$errors[] = ‘Your text file is too large!’;
}
return $errors;
}
function print_range($list,&$array) {
// print a table of characters from $low to $high. If the character is a key of $array, then its corresponding value is printed
// By reference, the array has all matched key=>value pairs removed
print “
“; print “ |
||
“;// print the character
print $glyph; |
“; print ord($glyph); print “ |
“;// if character exists print it and the number of occurances, then remove from array
if (array_key_exists($glyph, $array)) { print “ |
\n”;
}
function process_form() {
//This function is the core of funtionality
//Probably could be separated into smaller functions
//global $charlist;
$textsource = $_POST[‘textsource’];
$textsourcecopy = $textsource;
//
// BEGIN Count and remove ligatures
//
// Set up arrays
$base_ligs = array(‘ff’,‘fi’,‘fl’,‘ffi’,‘ffl’,‘ct’,‘st’,‘ae’,‘oe’);
$chosen_ligs = array();
//get ligs selected from the checkboxes, avoid grabbing other form properties
foreach ($_POST as $key => $value) {
if (in_array($key,$base_ligs)) {
$chosen_ligs[] = $value;
}
}
// Convert ligs typed by user into an array, then add all ligs together
$typed_ligs = explode(” “,$_POST[‘userLigatures’]);
$all_ligs = array_merge($chosen_ligs, $typed_ligs);
// sort the chosen ligatures by length
// create a new associative array where (key = ‘ligature’) and (value = length of ‘ligature’), then sort it
$user_ligs = array();
foreach ($all_ligs as $lig) {
$user_ligs[$lig] = strlen($lig);
}
arsort($user_ligs);
// Count and remove the ligatures
foreach ($user_ligs as $testlig => $length) {
// count occurances, then remove each occurance
$charlist[$testlig] = substr_count($textsource,$testlig);
$textsource = str_replace($testlig,”“,$textsource);
}
// Count and remove all remaining characters
while (strlen($textsource) > 0) {
// Grab one char from start of source
$testchar = $textsource{0};
// count occurances, then remove each occurance
$charlist[$testchar] = substr_count($textsource,$testchar);
$textsource = str_replace($testchar,”“,$textsource);
}
//
// END Count and remove ligatures
//
print <<
HTMLBLOCK;
//
// BEGIN Sort and print results
//
// Gather & print basic facts about text
$totalchars = strlen($textsourcecopy);
$totalwords = str_word_count($textsourcecopy);
$avgword = round($totalchars / $totalwords);
print ‘Total number of characters: ‘.$totalchars.’
‘;
print ‘Total number of words: ‘.$totalwords.’
‘;
print ‘Average word is ‘.$avgword.’ characters.’;
// Sort the characters, which are array keys, remember?
ksort($charlist);
$lowercase = range(‘a’,‘z’);
$uppercase = range(‘A’,‘Z’);
//$userlig = selections via interface
$points = explode(” “,”. , : ; – ‘ \” ! ? & * < > ( ) [ ] { } $ # @ % / | \\”);
$figures = explode(” “,“0 1 2 3 4 5 6 7 8 9 0”);
print_range($uppercase,$charlist);
print_range($lowercase,$charlist); // All lowercase printed and removed
print_range($points,$charlist);
print_range($figures,$charlist);
// Print all remaining glyphs
print “
“; print $glyph; print “ |
“; print ord($glyph); print “ |
“; print $count; print “ |
“;
}
//
// Begin main logic
//
if ($_POST[’_submit_check’]) {
// Form was submitted, time to validate
if ($form_errors = validate_form()) {
// There are errors, redisplay from with error messages
show_form($form_errors);
}
else {
// No errors, go ahead and process it
process_form();
}
}
else {
// Form was not submitted, time to print it
show_form();
}
//
//
// END main logic
//