Select a string function from the drop-down menu:
- Click Here -
str_repeat
str_shuffle
str_word_count
strlen
strrev
strtolower
strtoupper
strstr
substr
ucfirst
Enter the first argument here:
Enter a second argument here (if necessary):
Enter a third argument here (if necessary):
_END;
}
function processForm( )
{
if (isset($_POST['submitbtn'])) {
$fname = $_POST['fname'];
$arg1 = $_POST['arg1'];
$arg2 = $_POST['arg2'];
$arg3 = $_POST['arg3'];
echo "$fname($arg1, $arg2, $arg3) produces:";
$res = demo_fn($fname, $arg1, $arg2, $arg3);
echo "$res ";
}
}
function demo_fn($fname, $arg1, $arg2, $arg3)
{
$out = "";
if ($fname == "") {
$out = "*** ERROR: Empty function name. ";
}
else if ($arg1 == "") {
$out = "*** ERROR: First argument is empty. ";
}
else if ($arg2 == "") {
$out = demo_fn_one_arg($fname, $arg1);
}
else if ($arg3 == "") {
$out = demo_fn_two_args($fname, $arg1, $arg2);
}
else { //all arguments are present
$out = demo_fn_three_args($fname, $arg1, $arg2, $arg3);
}
return $out;
}
function demo_fn_one_arg($fname, $arg1)
{
$out = "";
switch ($fname) {
case "strlen":
$out = strlen($arg1);
break;
case "str_shuffle":
$out = str_shuffle($arg1);
break;
case "str_word_count": //a single variable used
$out = str_word_count($arg1);
break;
case "strrev":
$out = strrev($arg1);
break;
case "strtolower":
$out = strtolower($arg1);
break;
case "strtoupper":
$out = strtoupper($arg1);
break;
case "ucfirst":
$out = ucfirst($arg1);
break;
default:
$out = "*** ERROR: $fname needs second argument ";
}
return $out;
}
function demo_fn_two_args($fname, $arg1, $arg2)
{
$out = "";
switch ($fname) {
case "str_repeat":
$out = str_repeat($arg1, $arg2);
break;
case "str_word_count": //When only two variables are used
$out = print_r(str_word_count($arg1, $arg2));
break;
case "strstr": //Display the text after the needle
$out = strstr($arg1, $arg2);
break;
case "substr": //Unspecified length
$out = substr($arg1, $arg2);
break;
}
return $out;
}
function demo_fn_three_args($fname, $arg1, $arg2, $arg3)
{
$out = "";
switch ($fname) {
case "str_word_count": //If all three variables are used
$out = print_r(str_word_count($arg1, $arg2, $arg3));
break;
case "strstr": //display the text before the needle if labled 'true'
$out = strstr($arg1, $arg2, $arg3);
break;
case "substr": //Using a specified length
$out = substr($arg1, $arg2, $arg3);
break;
}
return $out;
}
?>