See if it suits 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 separate it at each spacing (to make it easier to compare) and at the end go comparing whether the array
of words entries have such words, according to which you give the answer, I had thought this thing of array
, because when I was doing I saw that the user can type hi or hello or others, so I implemented a function that only gets two arrays
, one with the system dictionary and the other with the user input, then it goes through the dictionary until finding a word that contains in the user input.
Example of inputs and outputs:
Entrance: Hello
Output: Hi.
Input: Hello what is your name ?
Exit: Hi, my name and Jose, pleasure.
How old are you ?
Exit: Ops, I was just born, so I’m no year old
Input: Who is your father ?
Exit: My father is Antonio.
Input: Bye Jose
Output: Bye Bye
Any doubt just comment.
Source of the accent strip: How to remove accent in upload with php?
How about? https://www.wired.com/2014/04/out-in-the-open-jasper/
– Reginaldo Rigo
A simpler solution is to use the method explodes, if I have time I’ll show you a simple example
– Leonardo