2015-04-27 15 views
1

假設我的目錄上有一個名爲table.pdf的文件,並且我試圖使用字符串table作爲搜索條件進行搜索。我試圖使用​​和table作爲我的$pattern,但這不會返回匹配,而使用table.pdf作爲$pattern返回匹配。示例使用fnmatch的文件名字符串搜索()

$table = 'table'; 
$table_2 = 'table.pdf'; 
if (fnmatch($table, $pdf)){ 
// No Match found 
} 
if (fnmatch($table_2, $pdf)){ 
// Match Found 
} 

如何解決此問題?

+1

嗨,根據文檔,**模式**是一個像字符串的shell,如果你想使用第一種模式,你應該去:「table *」如果你想匹配所有「表「文件。如果你的意思是有一個通用的PDF字符串,你應該去「* .pdf」。 –

回答

2

試試這個。

$table = 'table*'; 
$table_2 = 'table.pdf'; 
if (fnmatch($table, $pdf)){ 
// No Match found 
} 
if (fnmatch($table_2, $pdf)){ 
// Match Found 
}