1

如何在 PHP 中使用 preg_match 检查文件名?

文件名 = 2.pdf

如果另一个文件被调用,它也应该2-2.pdf匹配2-3.pdf

这是hypen之前的Id。它不需要检查.pdf文件扩展名。

$id = 2;
$list = scandir("D:/uploads/");
$list = preg_grep("/^".$id."$/", $list);

foreach ($list as $file)
{
    echo $file . "<br />";
}
4

2 回答 2

1

怎么样:

/^$id(?:-\d+)?/

解释:

/        : delimiter
^        : begining of strig
$id      : the id defined earlier
(?:      : begining of non capture group
    _    : a dash
    \d+  : one or more digits
)?       : end of group, optional
/        : delimiter

用法:

$list = preg_grep("/^$id(?:-\d+)?/", $list);
于 2014-10-12T12:09:11.743 回答
0

使用这个正则表达式

/^2[-.]/

演示:http ://regex101.com/r/aJ7cK0/1

于 2014-10-11T13:01:56.227 回答