glob
is a general term used to define techniques to match specified pattern according to rules related Unix shell. Linux and Unix systems and shells also supports glob and also provide function glob()
in system libraries. In this tutorial we will look glob()
function usage in PHP programming language.
glob
是一个通用术语,用于定义根据与Unix shell相关的规则匹配指定模式的技术。 Linux和Unix系统和外壳程序还支持glob,并在系统库中提供函数glob()
。 在本教程中,我们将研究PHP编程语言中glob()
函数的用法。
确切的字符串搜索 (Exact String Search)
We will start with a simple example. We will look how to match exact string or file name with a absolute path. In this example we will list file /home/ismail/poftut.c
. We can see example below that the function returns a list which contains matches.
我们将从一个简单的例子开始。 我们将研究如何将精确的字符串或文件名与绝对路径匹配。 在此示例中,我们将列出文件/home/ismail/poftut.c
。 我们可以在下面的示例中看到该函数返回一个包含匹配项的列表。
<?php
foreach(glob("/home/ismail/poftut.c") as $file){
echo "$file\n";
}
?>

通配符(Wildcards)
Wildcard is important glob operator for glob operations. Wildcard or asterisk is used to match zero or more characters. Wildcard specified that there may be zero character or multiple character where character is not important. In this exmaple we will match files those have .txt
extension.
通配符对于glob操作很重要。 通配符或星号用于匹配零个或多个字符。 通配符指定在字符不重要的情况下可以有零个字符或多个字符。 在本示例中,我们将匹配扩展名为.txt
文件。
<?php
foreach(glob("/home/ismail/*.txt") as $file){
echo "$file\n";
}
?>

As we can see that there are a lot of .txt
files those return in a PHP list.
我们可以看到,有许多.txt
文件返回到PHP列表中。
具有多级目录的通配符 (Wildcards with Multilevel Directories)
We can use wildcards in order to specify multilevel directories. If we want to search one level down directories for specified glob we will use /*/
. In this example we search for .txt
files in one level down directories in /home/ismail
.
我们可以使用通配符来指定多级目录。 如果要在一级目录中搜索指定的glob,则将使用/*/
。 在此示例中,我们在/home/ismail
下一级目录中搜索.txt
文件。

<?php
foreach(glob("/home/ismail/*/*.txt") as $file){
echo "$file\n";
}
?>
单字符 (Single Character)
There is a question mark which is used to match single character. This can be useful if we do not know single character for given name. In this example we will match files with files file?.txt
file where these will match
有一个问号,用于匹配单个字符。 如果我们不知道给定名称的单个字符,这将很有用。 在此示例中,我们将文件与文件file?.txt
文件匹配,这些文件将匹配
- file.txtfile.txt
- file1.txt file1.txt
- file5.txt file5.txt
- … …
<?php
foreach(glob("/home/ismail/*/*.?") as $file){
echo "$file\n";
}
?>

多个字符(Multiple Characters)
Glob also supports for alphabetic and numeric characters too. We can use [
to start character range and ]
is used to end character range. We can put whatever we want to match between square brackets. In this example we will match files and folders names those starts one of e,m,p
.
Glob还支持字母和数字字符。 我们可以使用[
来开始字符范围,而]
来结束字符范围。 我们可以将要匹配的任何内容放在方括号之间。 在此示例中,我们将匹配以e,m,p
之一开头的文件和文件夹名称。
<?php
foreach(glob("/home/ismail/[emp]*.tx?") as $file){
echo "$file\n";
}
?>

编号范围(Number Ranges)
In some cases we may want to match number range. We can use -
dash to specify start and end number. In this example we will match 0 to 9 with 0-9
. In this example we will match file and folder names those contains numbers from 0 to 9 .
在某些情况下,我们可能希望匹配数字范围。 我们可以使用-
破折号指定开始和结束编号。 在此示例中,我们将0到9与0-9
匹配。 在此示例中,我们将匹配文件和文件夹名称,其中包含从0到9的数字。
<?php
foreach(glob("/home/ismail/*[0-9]*") as $file){
echo "$file\n";
}
?>

字母范围(Alphabet Ranges)
We can also define Alphabet ranges similar to number ranges. we will use a-z
for lowercase characters where A-Z
for uppercase characters. What if we need to match lower and uppercase characters in a single statement. We can use a-Z
to match both lower and uppercase letters. In this example we will match files and folder names those starts with letters between a
and c
我们还可以定义类似于数字范围的字母范围。 我们将az
用于小写字符,将AZ
用于大写字符。 如果我们需要在单个语句中匹配大小写字符,该怎么办。 我们可以使用aZ
来匹配大小写字母。 在此示例中,我们将匹配以a
和c
之间a
字母开头的文件和文件夹名称
<?php
foreach(glob("/home/ismail/[a-c]*") as $file){
echo "$file\n";
}
?>

翻译自: https://www.poftut.com/php-glob-function-to-match-path-directory-file-names-with-examples/