Skip to content

Instantly share code, notes, and snippets.

@pkakelas
Created December 3, 2019 23:23
Show Gist options
  • Save pkakelas/6c8da2bb71ec5103fc64385e864ee094 to your computer and use it in GitHub Desktop.
Save pkakelas/6c8da2bb71ec5103fc64385e864ee094 to your computer and use it in GitHub Desktop.
body {
font-family: Arial, Helvetica, sans-serif;
}
#content {
width: 450px;
margin: 0 auto;
padding: 0px 20px 20px;
background: white;
border: 2px solid navy;
}
h1 {
color: navy;
}
label {
width: 10em;
padding-right: 1em;
float: left;
}
#data input {
float: left;
width: 15em;
margin-bottom: .5em;
}
#buttons input {
float: left;
margin-bottom: .5em;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
<?php
// get the data from the form
$phrase = $_POST['phrase'];
function getStringTypeComment($str) {
$type = getTypeOfString($str);
switch ($type) {
case 0:
return "Starts with 3 digits";
case 1:
return "Starts with 3 alphabetic characters";
}
return "Undefined";
}
// 0 => starts with 3 digits
// 1 => starts with 3 letters
// 2 => undefined
function getTypeOfString($str) {
$str = substr($str, 0, 3);
if (is_numeric($str)) {
return 0;
}
if (!is_numeric($str[0]) && !is_numeric($str[1]) && !is_numeric($str[2])) {
return 1;
}
return 2;
}
function createRow($str) {
$returnString = "<tr>\n";
$returnString .= "<td><span>" . $str . "</span></td>\n";
$returnString .= "<td><span>" . strlen($str) . "</span></td>\n";
$returnString .= "<td><span>" . getStringTypeComment($str) . "</span></td>\n";
$returnString .= "</tr>\n";
return $returnString;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional
...>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Phrase Parser</title>
<link rel="stylesheet" type="text/css" href="Phrase.css"/>
</head>
<body>
<div id="content">
<h1 align= center>Your phrase parsed into words </h1>
<table>
<tr>
<th>Word</th>
<th>Length</th>
<th>Type</th>
</tr>
<?php
$arr = explode(" ", trim($phrase));
foreach ($arr as $str) {
echo createRow($str);
}
?>
</table>
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Phrase Input</title>
<link rel="stylesheet" type="text/css" href="Phrase.css" />
</head>
<body>
<div id="content">
<h1 align ="center">Phrase Input</h1>
<div id="data">
<p>Enter a phrase consisting of at least 3 words separated by the character ' ' (space).</p>
<p>The words will be parsed and matched with the following types:</p>
<ol type="a">
<li>start with 3 alphabetic characters</li>
<li> start with 3 digits </li>
<li> undefined (are not of type WT1 or WT2 above).</li>
</ol>
</div>
<form action="PhraseParse.php" method="post" onsubmit ="checkForm(form)">
<label>Enter a Phrase:</label>
<input id="myphrase" type="text" name="phrase"/>
<label>Show table:</label>
<input id="TCode" type="submit" value="Submit Phrase" onclick="checkForm(form)"; />
</form>
</div>
</body>
<script>
function checkForm(form) {
let error = false
let phrase = document.getElementById("myphrase").value
let values = phrase.split(' ').filter(v => v !=='')
let re = /^[\w ]+$/;
// validation fails if the input is blank
if (phrase == "") {
error = true
alert("Error: Input is empty!")
}
// validation fails if the input doesn't match our regular expression
if (!re.test(phrase)) {
error = true
alert("Error: Input contains invalid characters! No special characters.")
}
//validation fails if the input is less than 3 words
if (values.length < 3) {
error = true
alert("Error: Input contains not enough words. Need at least 3 words or more.")
}
if (error) {
event.preventDefault()
return false
}
return true
}
</script>
</html>
<?php
// get the data from the form
$phrase = $_POST['phrase'];
$arr = explode(" ", trim($phrase));
function getStringTypeComment($str) {
$type = getTypeOfString($str);
switch ($type) {
case 0:
return "Starts with 3 digits";
case 1:
return "Starts with 3 alphabetic characters";
}
return "Undefined";
}
// 0 => starts with 3 digits
// 1 => starts with 3 letters
// 2 => undefined
function getTypeOfString($str) {
$str = substr($str, 0, 3);
if (is_numeric($str)) {
return 0;
}
if (!is_numeric($str[0]) && !is_numeric($str[1]) && !is_numeric($str[2])) {
return 1;
}
return 2;
}
function createRow($str) {
$returnString = "<tr>\n";
$returnString .= "<td><span>" . $str . "</span></td>\n";
$returnString .= "<td><span>" . strlen($str) . "</span></td>\n";
$returnString .= "<td><span>" . getStringTypeComment($str) . "</span></td>\n";
$returnString .= "</tr>\n";
return $returnString;
}
function generateStringExcluding($arr, $exclude) {
$phrase = [];
$unwantedType = $exclude == "wt1" ? 0 : 1;
foreach($arr as $str) {
$type = getTypeOfString($str);
if ($type != $unwantedType) {
$phrase[] = $str;
}
}
return join(" ", $phrase);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Phrase Parser</title>
<link rel="stylesheet" type="text/css" href="Phrase.css"/>
</head>
<body>
<div id="content">
<h1 align= center>Your phrase parsed into words </h1>
<table>
<tr>
<th>Word</th>
<th>Length</th>
<th>Type</th>
</tr>
<?php
foreach ($arr as $str) {
echo createRow($str);
}
?>
</table>
<form method="POST" action="./PhraseFilter.php">
<input type="hidden" name="phrase" value="<?php echo generateStringExcluding($arr, "wt1") ?>"></input>
<input type="submit" value="Exclude WT1">
</form>
<form method="POST" action="./PhraseFilter.php">
<input type="hidden" name="phrase" value="<?php echo generateStringExcluding($arr, "wt2") ?>"></input>
<input type="submit" value="Exclude WT2">
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment