PHP Glob() Function To Match Path, Directory, File Names with Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/php-glob-function-to-match-path-directory-file-names-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

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.

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.

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.

Wildcards
Wildcards

As we can see that there are a lot of .txt files those return in a PHP list.

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 .

Wildcards with Multilevel Directories
Wildcards with Multilevel Directories

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
  • file1.txt
  • file5.txt
LEARN MORE  Python Glob() Function To Match Path, Directory, File Names with Examples

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 .

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 .

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

Alphabet Ranges
Alphabet Ranges

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/php-glob-function-to-match-path-directory-file-names-with-examples/
PINTEREST
PINTEREST
INSTAGRAM