PHP search engine for txt files

0

I want to see if it is possible for the community to help me in this search engine that I am creating.

Let me explain what it is, is a set of txt files that are obtained from exporting the chats of the Whastapp application. The format of these chats are characterized by the fact that they start with the date, the name of the person making the comment or phone #, something like this:

  

05/09/18 6:54 a.m. - Elena Hidalgo Corredora: SKY GROUP SALE PRICE LOW REF100000REF AND REF40000REF   ☆☆☆☆ Land in hills of Guataparo of 1500M2 with perimeter fence, electric points, water tank, electric gate, bathrooms, advanced construction, main road and ready to sign. 300,000 NOW 100,000   Another 830M2 with perimeter fence in 60,000 Ref. NOW 40,000

I have the problem partially solved, on the one hand I have a search form where the person places something like this:

  

Tulipán San Diego 10,000 apartment

Any search, that's just an example. What done:

  • Through a php file, I collect the form information through the post method.
  • I convert this information into an array in order to find each word in the txt file.
  • I have converted the txt file into an array to iterate on each date, search in my opinion more easily and present the information ordered.
  • The results are acceptable, but it is not exactly what I am looking for, because it turns out that it iterates me word by word and shows as result all the keys that the word Apartment contains and if there is one that contains Apartment and Tulipán, it shows it 2 times , so it does with every word.

    What I want is for me to show only the keys that contain the 3 or 4 words that the form is indicating. Here he left the php code:

    //Capturamos la búsqueda
    $busqueda = $_POST['busqueda'];
    $fecha = $_POST['fecha'];
    
    $arrayb = explode (" ", $busqueda);  
    
    $array = explode("|", file_get_contents('chat'.$fecha.'.txt'));
    
    for($i=0 ;$i<count($array);$i++)
    {
    
        for($u=0 ;$u<count($arrayb);$u++)
        {
             $found=stripos($array[$i],$arrayb[$u]);
             if($found)
             {?>
                <div class='contaniner'>
                    <div class="row">
                        <div class="col">
                            <?php echo $array[$i];
                            echo "<br><br>";?>      
                        </div>
                    </div>
                </div>
    
                <?php 
             }         
        }
    }
    

    Here is the demo, only the first 5 dates of the month of September 2018 can be used. php search in flat files

        
    asked by Jean Carlo Garcia Quiñones 24.09.2018 в 08:52
    source

    1 answer

    0

    Leaving aside that it would be better to use some type of database that allows full-text search, I'll tell you how I think the filtering part of the elements should be so that only those that contain all of them appear. the words you've searched for

    To obtain the elements that contain all the words, we use a variable that controls the number of times it matches:     

    $arrayb = explode (" ", $busqueda);  
    $array = explode("|", file_get_contents('chat'.$fecha.'.txt'));
    $results = array();
    for ($i = 0; $i < count($array); $i++)
    {
        $line = $array[$i];
        // Reiniciamos el contador de coincidencias de palabras para esta línea (o entrada en el chat)
        $numberOfFounds = 0;
        for ($u = 0; $u < count($arrayb); $u++)
        {
            $found = stripos($line, $arrayb[$u]);
            if ($found)
            {
                // Si coincide, sumamos uno en el contador de coincidencias
                $numberOfFounds++;
            }
            else
            {
                // Si alguna palabra no coincide, dejamos de comprobar el resto para esa línea
                break;
            }
        }
        if ($numberOfFounds == count($arrayb))
        {
            // Si la línea (o entrada de chat) contiene todas las palabras, la añadimos al array de resultados
            array_push($results, $line);
        }
    }
    
    // Recorremos el array de resultados para mostrarlos al usuario
    foreach ($results as $result) {
    <div class='contaniner'>
        <div class="row">
            <div class="col">
                <?= $result ?><br><br>
            </div>
        </div>
    </div>
    } ?>
    
        
    answered by 24.09.2018 / 12:47
    source