Good day. There is a simple chat + bot in php. But he goes over the block + answer set in the script:

$responses['what is your name'] = "My name is Mo-Pal."; $responses['tell me about yourself'] = "I am a chatbot. I'm still learning a lot of things so please forgive me if I can't answer you in some cases."; $responses["i'm fine"] = "Good. I'm happy about that."; 

I would like to implement reading from a csv file. Suppose there are 2 stobs in csv. The column of what we write to the bot and the column of what the bot responds to.

Script code:

 <?php $responses['what is your name'] = "My name is Mo-Pal."; $responses['tell me about yourself'] = "I am a chatbot. I'm still learning a lot of things so please forgive me if I can't answer you in some cases."; $responses["i'm fine"] = "Good. I'm happy about that."; $q = $_GET["q"]; $response = ""; if ($q != "") { $q = strtolower($q); foreach ($responses as $r => $value) { if (strpos($r, $q) !== false) { $response = $value; } } } $noresponse = "Sorry I'm still learning. Hence my responses are limited. Ask something else."; echo $response === "" ? $noresponse : $response; ?> 

Any ideas for fellow experts? In PHP, while new :( Thanks in advance!

    1 answer 1

    Function str_getcsv, http://php.net/manual/en/function.str-getcsv.php

     <?php $csv = array_map('str_getcsv', file('data.csv')); ?> 
    • if the file is large - this option will be very slow - the correct solution will be read line by line, through fgets . - And
    • Then, too, for the sake of every line not to send a request for reading - Daniel Protopopov