亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 搜索并僅從 csv 文件返回完全匹配

PHP 搜索并僅從 csv 文件返回完全匹配

PHP
慕田峪9158850 2022-10-14 15:55:26
下面的代碼搜索我的 csv 文件,但當我只希望它返回精確時返回多個結果。例如; 在我的 csv 文件中,我在單獨的行中有以下內容:Henry Jones、Sarah Jones。如果我搜索 Sarah Jones,它會根據匹配的姓氏返回兩者。任何人都可以幫忙嗎?我嘗試了多種方法,但沒有任何樂趣!非常感謝您!<?phpif ( !isset( $_GET['q'] ) && !isset( $_GET['update'] ) ) {    echo '<div class="alert">' . $txt_hint . '</div>';}// Only on FORM Submitif ( 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;        }    }
查看完整描述

1 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

您可以使用 fullMatch 正則表達式和 fullMatch 標志嘗試類似的操作。


如果找到完全匹配,則觸發標志 fullMatch,然后在搜索中只能找到完全匹配


// 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;

    }

}

注意:這段代碼沒有優化,但它給了你一個想法!


查看完整回答
反對 回復 2022-10-14
  • 1 回答
  • 0 關注
  • 118 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號