Hello. There is a variable

$slova = "Рыба, Кот, Мышка, Дом"; 

How can I get each word separately and add it to the database, the table slova , to get something like this (in the database):

 id 1 | name Рыба id 2 | name Кот id 3 | name Мышка id 4 | name Дом 

I would be very grateful for the help and any useful information.

    3 answers 3

     $slova = "Рыба, Кот, Мышка, Дом"; $arr = explode(',', $slova); foreach ($arr as $word) { $word = trim($word); // Конечно лучше использовать подготовленные запросы, но для теста вот. // При условии, что поле `id` является AUTO_INCREMEMT $sql = "INSERT INTO `slova`(name) VALUES('$word')"; $insert = mysqli_query($link, $sql); } 

      There is a function like explode () . And the solution is quite simple.

       $slova = "Рыба, Кот, Мышка, Дом"; $slovo = []; $slovo = explode(',', $slova); print_r($slovo); 

        If there are no explicit delimiters, then you can do it differently, for example, if you divide by spaces:

         $toktext = preg_split("/[\s]+/", $text);