PHP:查找字符串中的所有文件名

PHP:查找字符串中的所有文件名,php,pattern-matching,filenames,Php,Pattern Matching,Filenames,我有很多嵌入文件名的字符串,比如“file”只是文件名的一个例子 $string1 = '?file.png#hello/file.pdf<____!.sss<>file.docx'; $string2 = '###file.txt#§§/FILE.png(((file.pdf**'; 如何编写函数string2filenames 已经尝试过使用preg_match_all和模式“/[a-zA-Z0-9-\]+\.[a-zA-Z]{2,4}/”,但没有成功。我想我找到了一些实

我有很多嵌入文件名的字符串,比如“file”只是文件名的一个例子

$string1 = '?file.png#hello/file.pdf<____!.sss<>file.docx';
$string2 = '###file.txt#§§/FILE.png(((file.pdf**';
如何编写函数string2filenames


已经尝试过使用preg_match_all和模式“/[a-zA-Z0-9-\]+\.[a-zA-Z]{2,4}/”,但没有成功。

我想我找到了一些实际可行的方法。 试试看

它将文件名与多个点和示例字符串以及一些更高级的字符串相匹配。 单击preg_match_all并查看[0]完全匹配。


编辑\w没有捕捉到-

也许不完美,但这是你的一个开始,名称始终是文件还是仅仅是巧合?@Andreas:这只是一个文件名示例好的。如果您知道文件名的扩展名,您可以缩小搜索范围,并使用say/\w+\.[docx | xlsx | PDF]/等来改进模式。或者如果你知道只有三个和四个字母的扩展名,文件名没有点/\w+\。\w{3,4}/@WeSee你看到我的答案了吗?
$first = string2filenames($string1);
// $first should be [ 'file.png','file.pdf','file.docx' ]

$seconnd = string2filenames($string1);
// $seconnd should be [ 'file.txt','FILE.png','file.pdf' ]
$pattern ="/([A-Za-z0-9 _-]+\.)+(\w{3,4})/";