See if it caters to you:
<form acton="#" method="post">
<input name="speak" />
<button>Go!</button>
</form>
<?php
function clearId($id){
$special = Array('Á','È','ô','Ç','á','è','Ò','ç','Â','Ë','ò','â','ë','Ø','Ñ','À','Ð','ø','ñ','à','ð','Õ','Å','õ','Ý','å','Í','Ö','ý','Ã','í','ö','ã',
'Î','Ä','î','Ú','ä','Ì','ú','Æ','ì','Û','æ','Ï','û','ï','Ù','®','É','ù','©','é','Ó','Ü','Þ','Ê','ó','ü','þ','ê','Ô','ß','‘','’','‚','“','”','„');
$clearspc = Array('a','e','o','c','a','e','o','c','a','e','o','a','e','o','n','a','d','o','n','a','o','o','a','o','y','a','i','o','y','a','i','o','a',
'i','a','i','u','a','i','u','a','i','u','a','i','u','i','u','','e','u','c','e','o','u','p','e','o','u','b','e','o','b','','','','','','');
$newId = str_replace($special, $clearspc, $id);
return strtolower($newId);
}
function comparaComArray($array, $input) {
foreach($array as $value)
if(in_array($value, $input))
return true;
return false;
}
if(count($_POST) > 0) {
$words = strtolower(clearId($_POST['speak']));
$words = explode(' ', $words);
$saudacoes = array('oi', 'ola', 'ei', 'hello');
if(comparaComArray($saudacoes, $words) and !in_array('nome', $words))
echo "Oi. <br>";
else if(comparaComArray($saudacoes, $words) and in_array('nome', $words))
echo "Oi, meu nome e jose, prazer. <br>";
else if(in_array('tchau', $words))
echo "Bye Bye. <br>";
else if(in_array('seu', $words) and in_array('pai', $words) and in_array('?', $words))
echo "Meu pai é o Antonio. <br>";
else if(in_array('quantos', $words) and in_array('anos', $words) and in_array('voce', $words))
echo "Ops, acabei de nascer, então tenho nenhum ano de idade<br>";
}
?>
Basically the code flow is to collect a text field from the form, remove the accent from it separating it into each spacing (to make comparisons easier) and in the end go comparing whether the
array
of words entered have such words , according to this you give the answer, I had thought this
array
thing, because when I was doing that I saw that the user could type hi or hello or others, so I implemented a function that only receives two
arrays
, one with the system dictionary and another one with the user input, then it scrolls through the dictionary until it finds a word that contains the user input.
Example of inputs and outputs:
Entry: Hello
Output: Hi.
Entry: Hello, what's your name?
Output: Hi, my name is Jose, pleasure.
How old are you?
Output: Oops, I just got born, so I'm one year old
Entry: Who is your father?
Output: My father is Antonio.
Admission: Bye-bye
Output: Bye Bye
Any questions just comment.
Font of accent strip: How to remove accent in upload with php?