From the course: PHP Techniques: Working with Files and Directories

Pattern matching with glob - PHP Tutorial

From the course: PHP Techniques: Working with Files and Directories

Start my 1-month free trial

Pattern matching with glob

- [Instructor] There's a completely different technique for working with directory entries in PHP which is powerful and worth knowing about. We can use something called glob, G-L-O-B to do pattern matching. You call the PHP function glob and you pass in a pattern as an argument. It returns an array of all paths to files and directories that match that pattern. It returns an array of entries, but those entries may be in several directories, not just in one. It's a good way to scan for files or directories that match a pattern. To write a pattern, we need to learn the syntax. Any regular character will match the same character in the file name but there are a few special characters. They aren't the same as regular expressions but it's a similar idea. The question mark is a wild card that matches any one character. If you know regular expressions it's the same as a single dot any character. If we use the asterisk, it represents any zero or more characters. It's like saying, "I don't care what might or might not be in this part of the path." It's similar to the regular expression version with a dot and an asterisk after it. We can use square brackets to match any character inside the set, or put an exclamation point at the start to not match any character in the set. And if we want our pattern to match a literal question mark asterisk or bracket, we can put a backslash in front of it to take away its special meaning. Let's try an example. Let's create a new file and let's call it da_glob.php, I'll put my php tags inside of it. And let's start out by just calling glob and we're going to catch whatever it returns to us. I'll come back to the pattern in a moment whenever it returns to us, I'm going to catch in entries because it returns in array to us. And then let's take a look at what's inside that array using a foreach loop. And in that foreach loop, we'll save for entries as entry. So that'll then give us an entry that we can look at and br tag at the end. So we'll get the same effect as we had before. We'll get all of the entries and we'll list them with br tags one after another. Now what should we put in here as our pattern? To start with, let's do file_????? and then five question marks and then dot php. All of the regular characters like F-I-L-E those are going to match the exact characters over here. So you can see it's going to match F-I-L-E. The question mark is going to be any one character. So I'm looking for five characters in a row and then .php. Let's try it out. We'll save it. We'll go over to Firefox. Let's try da_glob.php. You'll see there're two of them, file_lines and file_write. It didn't match all the other ones though because they had a different number of characters. This requires it must be five characters. It doesn't care what the five characters are but there must be five characters. If I instead use the asterisk that says, "I don't care what the characters are," it could be nothing or it could be a million characters file_ and then something .php. So let's save it. We'll come back over here. And you'll see now we get everything that starts with the word file. If we try and example with character sets, we could put a character set around F now it'll match anything with file. If I put a T there and that'll match anything with file or tile. Now there's a kind of a contrived example cause I don't have anything in my directory called tile but it works the same way. We can come over here and we reload it and get the same results. You could also for example, do asterisk .txt, it's very common. I don't care what the file name is, I care about its extension. I'm looking for anything that it's the .txt file, I'll save it. I'll come back over here and reload and you'll see that it returned four to me. If we look over here you'll see these four files that are right here at the bottom. Notice it did not return these to me because it's looking at the path and the path it did not include directory changes in that. That's an important point. It's anything except the change of directory. If I want to look in another directory I can do the star/star. Now this will say anything that's nested in any directory regardless of its name, that ends in .txt. So I'll save it, I'll reload it and you'll see here now return anything that's in the assets directory. So I am getting back those two files, but I'm no longer getting these because they don't match. Their path doesn't have that slash in front of it. The glob function is a powerful tool for scanning the file system for matching entries across many directories.

Contents