Search code examples
phpregexpreg-match

PHP search and return exact matches only from a csv file


The below code searches my csv file but returns multiple results when I only want it to return exact. For example; in my csv file I have the following on separate lines: Henry Jones, Sarah Jones. If I search for Sarah Jones it is returning both based upon the matching surname. Can anyone help? I have tried multiple things but with no joy! Thank you very much in advance!

<?php
if ( !isset( $_GET['q'] ) && !isset( $_GET['update'] ) ) {
    echo '<div class="alert">' . $txt_hint . '</div>';
}
// Only on FORM Submit
if ( isset( $_GET['q'] ) && !empty( $_GET['q'] ) ) {
    // Remove the Regex Char
    $words = str_replace( '#', '', $_GET['q'] );
    // Open CSV.File
    $file  = fopen( $CSV_Filename, 'r' );
    // Supports any Number of Search-Words
    $words = explode ( ' ', $words );    
    // Make the Search-Words safe to use in Regex (escapes special characters)
    $words = array_map( 'preg_quote', $words );
    // Make Regex e.g. '/Project|Name/i' means 'Project or Name' case (i)nsensitive
    $regex = '#' . implode( '|', $words ) . '#i';







    // Set Skip-First-Line Helper
    $flag = true;
    // Loop each Line
    while ( ( $line = fgetcsv( $file ) ) !== FALSE ) {
        // Skip first Line (only Healine, no real Data)
        if ( $flag ) {
            $flag = false;
            continue;
        }
        // Split Line
        list( $Details_1,$Time_Started,$Details_3,$Job_Name,$Time_Complete ) = $line;
        // Check if Search-Match AND $Time_Complete is empty
        if ( preg_match( $regex, $Job_Name ) && $Time_Complete == '' ) {
            // Show Data in Browser
            echo '
              <div class="box">' . $Time_Started . '</div>
              <div class="box">' . $Details_3 . '</div>
              <div class="box">' . $Job_Name . '</div>

              <div class="box noBorder"><a class="update" 
               href="' . htmlentities($_SERVER['PHP_SELF']) . '?update=' .
               htmlentities( urlencode( $Job_Name ) ) . '">' . 
               $txt_completebutton . '</a></div><br>
            ';
            // Set Search-Hit Helper
            $hit = true;
        }
    }

Solution

  • You can try something like that with a fullMatch regex and a fullMatch flag.

    If a full match is found, the flag fullMatch is triggered and then only full match can be found in the search

    // Make Regex e.g. '/Project|Name/i' means 'Project or Name' case (i)nsensitive
    $regex = '#' . implode( '|', $words ) . '#i';
    $regexFullMatch = '#' . implode( '', $words ) . '#i';
    $isFullMatched = true;
    .
    .
    .
    if ( preg_match( $regex, $Job_Name ) && $Time_Complete == '' ) {
          if(preg_match( $regexFullMatch, $Job_Name )){
            // Show Data in Browser
            echo '
              <div class="box">' . $Time_Started . '</div>
              <div class="box">' . $Details_3 . '</div>
              <div class="box">' . $Job_Name . '</div>
    
              <div class="box noBorder"><a class="update" 
               href="' . htmlentities($_SERVER['PHP_SELF']) . '?update=' .
               htmlentities( urlencode( $Job_Name ) ) . '">' . 
               $txt_completebutton . '</a></div><br>
            ';
            // Set Search-Hit Helper
            $hit = true;
            $isFullMatched = true;
        }
        else if(!$isFullMatched){
            // Show Data in Browser
            echo '
              <div class="box">' . $Time_Started . '</div>
              <div class="box">' . $Details_3 . '</div>
              <div class="box">' . $Job_Name . '</div>
    
              <div class="box noBorder"><a class="update" 
               href="' . htmlentities($_SERVER['PHP_SELF']) . '?update=' .
               htmlentities( urlencode( $Job_Name ) ) . '">' . 
               $txt_completebutton . '</a></div><br>
            ';
            // Set Search-Hit Helper
            $hit = true;
        }
    }
    

    Note : this code is not optimized, but it give you an idea !