String function name:
Argument 1:
Argument 2:

_FORM; } //-----------------Process Form------------------- //Input: POST data. //Output: The result of the desired implemented string function. function processForm() { if (isset($_POST['show_result'])) { $str_fun = htmlentities($_POST['str_function']); $arg1 = htmlentities($_POST['arg1']); $arg2 = htmlentities($_POST['arg2']); $str_output = demo_fn($str_fun, $arg1, $arg2); echo "Function requested: $str_fun
\n"; echo "Arguments specified: $arg1"; if ($arg2 != "") echo " and $arg2
\n"; else echo "
\n"; echo "The end result is $str_output"; } } //-----------------Demo Function Initialization------------------- //Input: Function name and one or two arguments. //Output: One string. function demo_fn($str_fun, $arg1, $arg2) { if ($str_fun == "") return "an error. You didn't specify a function.\n"; else if ($str_fun == "furby") return "a furby that is not amused at what you typed."; else if ($arg1 == "") return "an error. There are no functions here that take zero arguments.\n"; else if ($arg2 == "") return demo_fn_1arg($str_fun, $arg1); else return demo_fn_2arg($str_fun, $arg1, $arg2); } //-----------------Demo Function ~ One Argument------------------- //Input: Function name and one argument. //Output: One string. function demo_fn_1arg($str_fun, $arg1) { switch ($str_fun) { case "str_repeat": return "an error. " . str_repeat("This function takes two arguments, not one.\n", 2); case "str_shuffle": return str_shuffle($arg1); case "str_word_count": return str_word_count($arg1); case "strlen": return strlen($arg1); case "strrev": return strrev($arg1); case "strtolower": return strtolower($arg1); case "strtoupper": return strtoupper($arg1); case "strstr": return "an error. This program didn't find the second argument.\n"; case "substr": { $oh_no = "an error. \n"; for ($zapper=0; $zapper <= strlen("This function takes two arguments, not one. "); $zapper++) $oh_no .= substr("This function takes two arguments, not one. ", $zapper); return $oh_no; } case "ucfirst": return ucfirst($arg1); default: return "an error. The function is not in our 1 argument directory.\n"; } } //-----------------Demo Function ~ Two Arguments------------------- //Input: Function name and two arguments. //Output: One string. function demo_fn_2arg($str_fun, $arg1, $arg2) { switch ($str_fun) { case "str_repeat": //PHP document specifies the second argument is an integer and needs to be zero or positive: check that here. { if (is_numeric($arg2) && $arg2 >= 0) return str_repeat($arg1, $arg2); else if (is_numeric($arg2)) return "an error. " . str_repeat("$str_fun does not take a negative argument number.\n", 0-$arg2); else return "an error. " . str_repeat("Second argument is not a number.\n", 2); } case "str_shuffle": return "an error. " . str_shuffle("This function takes one argument, not two.") . "\n"; case "str_word_count": { //PHP document specifies the second argument is a format number. Check for a valid format and process accordingly. if ($arg2 > 2 || $arg2 < 0 || !is_numeric($arg2)) return "an error. You have specified an invalid format ID.\n" . str_word_count("an error. You have specified an invalid format.\n"); else if ($arg2 != 0) return var_export(str_word_count($arg1, $arg2), true); else return str_word_count($arg1, $arg2); } case "strlen": return "an error. This function takes one argument, not two.\n" . strlen("an error. This function takes one argument, not two.\n"); case "strrev": return "an error. " . strrev("This function takes one argument, not two.\n"); case "strtolower": return "an error. " . strtolower("This function takes one argument, not two.\n"); case "strtoupper": return "an error. " . strtoupper("This function takes one argument, not two.\n"); case "strstr": { //Give the user something more informative than a zero if the string is not found. if (!strstr($arg1, $arg2)) return "not found."; else return strstr($arg1, $arg2); } case "substr": { //PHP document specifies the second argument is an integer: check that here. if (is_numeric($arg2)) { //Give the user something more informative than... nothing if the sub-string returns nothing. if(!substr($arg1, $arg2)) return "a string that has been erased from existence."; else return substr($arg1, $arg2); } else { $oh_no = "an error. \n"; for ($zapper=0; $zapper <= strlen("Second argument is not a number. "); $zapper++) $oh_no .= substr("Second argument is not a number. ", $zapper); return $oh_no; } } case "ucfirst": return "an error. " . ucfirst("this function takes one argument, not two.\n");; default: return "an error. The function is not in our 2 argument directory.\n"; } } ?>