为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 163 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号