0
我有一個表單,用戶可以在其中輸入文件名。它遍歷所有目錄,成功地將用戶搜索輸入匹配到相關的pdf文件。不想向用戶顯示所有RecursiveDirectoryIterator文件
當它找到匹配它正確回聲'它匹配'並突破foreach循環。但是,它也正確地指出它找到的所有文件在匹配正確的文件之前「不匹配」。所以我得到了一個很長的「不匹配」列表,然後是「匹配」。
如果我回顯「」而不是'不匹配',它工作正常,不會顯示任何內容,但我只想告訴用戶一次他們輸入的內容不匹配。我確信我忽視了一些基本的東西,但是如何實現這一目標,我們將不勝感激任何幫助。
這是我的代碼。
<?php
if (isset($_POST['submit']) && !empty($_POST['target'])) {
$searchInput = $_POST['target'];
$it = new RecursiveDirectoryIterator("/myDirectory/");
foreach(new RecursiveIteratorIterator($it) as $file) {
$path_info = pathinfo($file);
$extension = $path_info['extension'];
if (strcmp("pdf", $extension) == 0) {
$lowerInput = strtolower($searchInput);
if (!empty($path_info)) {
$string = strtolower($path_info['filename']);
if(preg_match("~\b" . $lowerInput. "\b~", $string)) {
echo "it matches <br>";
break;
} else {
if (!preg_match("~\b" . $lowerInput . "\b~", $string)) {
echo "not a match <br>";
}
}
}
}
} //end foreach
} // end if submit pressed
?>
<html>
<head>
</head>
<body>
<h3>Search Files</h3>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="searchform">
Type the File You Require:<br><br>
<input id="target" type="text" name="target" required >
<br>
<input id="submit" type="submit" name="submit" value="Search" >
</form>
</body>
</html>
謝謝你完美的作品。總的來說,刪除else子句。再次感謝!!! – Harlin