PHP Glob()函数以示例匹配路径,目录,文件名

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"; 
} 
?>
Exact String Search
Exact String Search
确切的字符串搜索

通配符(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"; 
} 
?>
Wildcards
Wildcards
通配符

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文件。

Wildcards with Multilevel Directories
Wildcards with Multilevel Directories
具有多级目录的通配符
<?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.txt

    file.txt
  • file1.txt

    file1.txt
  • file5.txt

    file5.txt
<?php 
foreach(glob("/home/ismail/*/*.?") as $file){ 
 echo "$file\n"; 
} 
?>
Single Character
Single Character
单字符

多个字符(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"; 
} 
?>
Multiple Characters
Multiple Characters
多个字符

编号范围(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"; 
} 
?>
Number Ranges
Number Ranges
编号范围

字母范围(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来匹配大小写字母。 在此示例中,我们将匹配以ac之间a字母开头的文件和文件夹名称

<?php 
foreach(glob("/home/ismail/[a-c]*") as $file){ 
 echo "$file\n"; 
} 
?>
Alphabet Ranges
Alphabet Ranges
字母范围
LEARN MORE  Linux File System and Directories
了解更多Linux文件系统和目录

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值