【发布时间】:2017-07-14 20:41:19
【问题描述】:
我有很多嵌入文件名的字符串('file' 只是文件名的一个例子)
$string1 = '?file.png#hello/file.pdf<____!.sss<>file.docx';
$string2 = '###file.txt#§§/FILE.png(((file.pdf**';
文件名是有效的 Linux 或 Windows 文件名。
我需要从字符串中提取所有文件名(不包括路径)到一个数组中,例如:
$first = string2filenames($string1);
// $first should be [ 'file.png','file.pdf','file.docx' ]
$seconnd = string2filenames($string1);
// $seconnd should be [ 'file.txt','FILE.png','file.pdf' ]
函数string2filenames怎么写?
已经尝试使用 preg_match_all(使用模式 '/[a-zA-Z0-9-_]+\.[a-zA-Z]{2,4}/')但没有成功。
【问题讨论】:
-
regex101.com/r/NesfXO/1 可能并不完美,但对你来说是一个开始
-
这个名字总是文件还是只是巧合?
-
@Andreas:这只是一个示例文件名
-
好的。如果您知道文件名的扩展名,您可以使用“/(\w+\.[docx|xlsx|PDF])/”等来缩小搜索范围(并改进模式)。或者,如果您知道它只有三个和四个字母扩展名并且文件名没有点 "/(\w+\.\w{3,4})/"
-
@WeSee 你看到我的回答了吗?
标签: php pattern-matching filenames