PHP String Search Functions0:00
So far, we've almost entirely been focusing on the regular expression syntax, but now I'd like to switch it back over to PHP and learn about some of the various functions that we have available to us. For example, imagine that we have this message right here, and we want to determine if the string contains the word centauri. Well, how could we do that? And remember, only reach for a regular expression when you have no other choice. So in this case, well, there's a couple of things we could do. We could, to start, use the strpos function. So if we say message, and we're looking for centauri, and if that does not equal false, then we were able to find it. So I will var_dump and say we found it, and if we run that, sure enough, you can see it right there. Perfect. So we didn't even need a regular expression there. Now another option would be to do something like this. We could use str_contains. Once again, pass in the message and look for centauri,
When to Use preg_match0:52
So we didn't even need a regular expression there. Now another option would be to do something like this. We could use str_contains. Once again, pass in the message and look for centauri, and now if we run it again, that still works. And then alternatively, if this was lowercase, well, if we run it now, we're not going to find it. So we could use str_ireplace, and that just means whether it's uppercase or lowercase doesn't matter. So we run it again, and we're back to finding it. Now though, what if they did not spell it properly? Meaning you're filtering through some kind of comment section, and in some cases it's spelled like this, and in others the u is omitted. Well, if you still want to match it, it's times like these when we need to resort to a regular expression. Let me show you. We will switch over to the preg_match function. So we give our pattern here, and now I will spell it correctly, but specify that the u is optional. Now let's compare
expression. Let me show you. We will switch over to the preg_match function. So we give our pattern here, and now I will spell it correctly, but specify that the u is optional. Now let's compare that against the message, and once again, if you could find it, then we'll say we found it. So we run it, and there you go. Now while we are on the subject of preg_match, let's cover something real quick. You might think that you can do something like this, where we save all the matches here, and then if we var_dump the matches and run it, well, you get 1. So what is that referring to? Well, basically if we did find a match, then we return 1. If we didn't, then we return a 0. Now if you want to actually fetch the matches, then you need to specify a variable as the third argument, like so. Now if we save and run it again, there we go. We get our matches. However, if you think about it, even if we make this capital C and we run it, we're still only getting
Matching vs Replacing Text2:26
argument, like so. Now if we save and run it again, there we go. We get our matches. However, if you think about it, even if we make this capital C and we run it, we're still only getting the one match when we expect to get this one as well as that one. And that's because this function here will stop after the first match. So if you wanted to keep going, sort of like the G flag from our regular expressions training, then you want to make sure to do preg_match_all. So now if we run it, we'll have an array of two items here. But let's continue on. What if we aren't interested in actually matching the strings, but instead we're concerned with replacing them? Well, in those cases, we use preg_replace. And we've already covered this a little bit in the series. So remember, by default, preg_replace will assume that you have the G flag turned on. So it will replace every occurrence. And also notice we have a
in the series. So remember, by default, preg_replace will assume that you have the G flag turned on. So it will replace every occurrence. And also notice we have a slight variation preg_replace_callback. And this is when you want to create a closure so that you have a little more control over how you replace whatever is matched. Anyhow, what we have here is fine. So we want to search for this string and instead replace it with what? Why don't we just say John. Finally, as the third param, we feed it our body or the message. So if we save that to replaced, and now we run it, let's give it another try. And there you go. Now we've updated every occurrence, regardless of whether it was spelled correctly or incorrectly. Now let's review a different example. Imagine that you have some kind of sequence here. For example, foo, and then bar, and then baz. And you want to split that into an array. Well, we could say this array
Splitting Strings with preg_split4:00
review a different example. Imagine that you have some kind of sequence here. For example, foo, and then bar, and then baz. And you want to split that into an array. Well, we could say this array equals explode, using a comma as a separator, the message. And now once again, if we var_dump the array and run it, sure enough, you get three keys here. However, there's one problem with this. What if in some cases there will be a space after the comma? Well, if we run it now, notice that we get that extra space there. And the same would be true, for example, if we have a space after the word. So run it, and now, yeah, we still get the three items, but we have all these extra spaces that we're not interested in. So once again, this is where we can switch over to using a regular expression. Now, rather than explode, I will do a preg_split. And the only difference is, well, let's just reproduce what we had before. So if we run it, there we go,
to using a regular expression. Now, rather than explode, I will do a preg_split. And the only difference is, well, let's just reproduce what we had before. So if we run it, there we go, it still works. But now I will specify that we want a space, but that's optional. So you could do something like that. And here we're basically saying split by a comma, and a space before or after it is optional. So now if we run it, there we go, it works. But let's keep going. What if we really can't control how many spaces there are before the comma? Well, now if we run it again, yep, that's not working like we want. So it sounds like we need to say any number of spaces before it is fine in this particular example. So rather than the ?, we can use *. And if you remember what that means, we're saying zero or more of the preceding character. So in translation, zero or more spaces. And remember, if you did a +, the only difference between these two
you remember what that means, we're saying zero or more of the preceding character. So in translation, zero or more spaces. And remember, if you did a +, the only difference between these two is that the plus will expect at least one occurrence. A star says there could be zero or 10. It doesn't matter. So now if we run it, everything's working correctly again. Now one more thing about the space. Yes, we can do it like this, or I can do the explicit \s to refer to a space character. So if we run it again, there we go. So you can sort of think of these preg* functions as the existing string functions, but sort of on steroids so that you have a lot more control over what specifically you're matching. Let's do one more example together. Imagine that we have, and let me close this out, a comments array, maybe something that you're fetching from a MySQL database. And you want to very easily filter down these comments to ones
Filtering Arrays with preg_grep6:33
Imagine that we have, and let me close this out, a comments array, maybe something that you're fetching from a MySQL database. And you want to very easily filter down these comments to ones that refer to some particular string. And let's also assume that you're not doing this kind of logic on the MySQL end. Well, how could we do that? And what I'll do is quickly pause and create some test comments for us to work with. All right, so we have four comments here, and now we want to filter them down to only the comments that reference Centauri in some way. And once again, whether it's spelled correctly or incorrectly doesn't matter. We still want to match it. So how would we go about doing that? Why don't you pause the video and see how you might do it? Okay, let's do a couple together. Our first thought might be to use something like array_filter. So maybe something like this. Filter over the comments, and for each comment, we just want to
Okay, let's do a couple together. Our first thought might be to use something like array_filter. So maybe something like this. Filter over the comments, and for each comment, we just want to determine if it contains the word Centauri. So maybe we could return preg_match, and then once again look for Centauri, but the U is optional. And then I will use the I flag because capitalization doesn't matter. And compare that against the actual comment. And that should do it. So now, if we var_dump comments and run it, there you go. We only get those two comments. So it's true, that would work, but you know what? There's an easier way. Instead, we're going to use the preg_grep function. So what this will do, well, let's just switch over and look at it. It will return an array of only the entries that match some kind of pattern. So think of that as a way to say, filter through these items and only return the ones that match the pattern I give it.
an array of only the entries that match some kind of pattern. So think of that as a way to say, filter through these items and only return the ones that match the pattern I give it. So let's try it out. comments equals preg_grep. The pattern, once again, will be what we did before. And we will give it the input. And that's it. We're done. Nothing else to do there. So if we run it, we get the exact same output. So hopefully you can see how useful these various functions can be. All right. So keep playing around with these functions on your own. And if you have any questions, leave a comment below this video.
