PHP 搜索和 return 仅来自 csv 文件的完全匹配
PHP search and return exact matches only from a csv file
下面的代码搜索我的 csv 文件,但 return 有多个结果,而我只希望它 return 准确。例如;在我的 csv 文件中,我在不同的行中有以下内容:Henry Jones、Sarah Jones。
如果我搜索 Sarah Jones,它会根据匹配的姓氏 returning 两者。
谁能帮忙?我尝试了很多东西,但没有快乐!非常感谢您!
<?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;
}
}
您可以使用 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;
}
}
注意:这段代码没有优化,但它给了你一个想法!
下面的代码搜索我的 csv 文件,但 return 有多个结果,而我只希望它 return 准确。例如;在我的 csv 文件中,我在不同的行中有以下内容:Henry Jones、Sarah Jones。 如果我搜索 Sarah Jones,它会根据匹配的姓氏 returning 两者。 谁能帮忙?我尝试了很多东西,但没有快乐!非常感谢您!
<?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;
}
}
您可以使用 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;
}
}
注意:这段代码没有优化,但它给了你一个想法!