[英]How to find pathnames matching a pattern using PHP
我正在尋找目錄中的文件。 我的目錄路徑名模式如下所示:
TEST_1.jpg
TEST_2.jpg
TEST_3.jpg
TEST_4.jpg
twPL_1.jpg
twPL_2.jpg
twPL_3.jpg
twPL_4.jpg
所以如果我有一個localhost:8000 / covers的URL參數? customer = twPL 。 我正在獲取前兩個jpg,並且正在工作。 問題在於URL參數區分大小寫。 如果我嘗試用小寫字母(...? customer = twpl )編寫URL Paramater,我會收到一個通知:
注意:未定義的偏移量:0
注意:未定義的偏移量:1
<?php
if(isset($_GET["customer"])) {
$customer = $_GET["customer"];
$dirs = glob("cover/".$customer."*.jpg");
$allData = glob("cover/*.jpg");
$killExt = array("cover/","_1","_2","_3","_4",".jpg");
$new_array = str_replace($killExt, "", $allData);
$arrayLowerCase = array_map('strtolower', $new_array);
// die(var_dump($dirs));
if(array_search(strtolower($customer), $arrayLowerCase) !== false){
for($i=0; $i < 2; $i++) {
echo
'<div class="cover" data-page-number="0">',
'<img class="front-modal" src="'.strtolower($dirs[$i]).'"/>',
'</div>';
}
}
}
?>
我認為這會導致問題$dirs = glob("cover/".$customer."*.jpg");
我如何以不區分大小寫的方式在此glob函數中進行搜索...
我認為最好的方法是從比您定義的規則開始:使用小寫或大寫的名稱。 因此,僅小寫所有$ _GET參數比執行您想做的要容易。 隨你。
對於您的問題,您可以通過正則表達式搜索與兩個文件名匹配的所有模式:TEST:Test,TEsT等twPL:twpl,TWPL等
//this will find all pattern of twpl
preg_match("/twpl/i", $_GET["customer"], $matches_twpl);
preg_match("/test/i", $_GET["customer"], $matches_test);
接着
$safe_cust = "";
if(strtolower($matches_twpl[0]) == "twpl"){
$safe_cust = "twPL";
} else if ( strtolower($matches_test[0]) == "test") {
$safe_cust = "TEST";
}else {
//error
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.