PHP GLOB more than one pattern at a time

心已入冬 提交于 2019-12-11 17:52:38

问题


code:

$matches = glob("$searchword*.txt",  GLOB_BRACE)  ;

that works, but i also have $secondword, so i read How to define multiple patterns in php glob()

so i tried

$matches = glob("{$searchword},{$secondword}*.txt",  GLOB_BRACE)  ;
$matches = glob("{$searchword,$secondword}*.txt",  GLOB_BRACE)  ;
$matches = glob("{$searchword*.txt},{$secondword*.txt}",  GLOB_BRACE)  ;

$matches = glob("$searchword*.txt",  GLOB_BRACE) && ("$secondword*.txt",  GLOB_BRACE);
$matches = (glob("$searchword*.txt",  GLOB_BRACE) && ("$secondword*.txt",  GLOB_BRACE));

results in invalid syntax

what im trying to do: list files via glob that are smilar to a $filename is it possible to glob files that are similar to $filename?

references: https://www.cowburn.info/2010/04/30/glob-patterns/


回答1:


I've multiple txt files on my directory. Here I used glob with GLOB_BRACE and it perfectly showing the expected result to me.

$a = 'import_start_';
$b = '22_02_18_country_';
$m= glob("{{$a},{$b}}*.txt",  GLOB_BRACE);
 // OR // 
// $m= glob({".$a.",".$b."}*.txt", GLOB_BRACE);              
// OR //

print '<pre>';
print_r($m);
print '</pre>';

Output:

Array
(
 [0] => import_start_date.txt
 [1] => 22_02_18_country_list.txt
)


来源:https://stackoverflow.com/questions/49314061/php-glob-more-than-one-pattern-at-a-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!