تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Filtering Bad Words0:00

Visit any YouTube comment section. It doesn't matter what the video is. Just view the comment section and you're bound to find some incredibly cruel and mean comment. Well, let's imagine that for your website, you decide that you want to filter out any bad words, and you just want to do a quick search and replace on a little lookup. Now, in this case, for the screencast, I'm using pretty tame words, but you get the basic idea, right? Well, how might we accomplish that with a regular expression? Well, let's begin with the first one. Maybe we've decided that a word like stupid is over the line in your opinion, and we want to change any occurrence of that to amazing. Well, to begin, remember, keep it as simple as you possibly can until you need to expand what you capture. So in this case, if stupid was the only word, then you could look for that exactly. Look for an S followed by a T followed by a U. Remember, in order for this to capture,

expand what you capture. So in this case, if stupid was the only word, then you could look for that exactly. Look for an S followed by a T followed by a U. Remember, in order for this to capture, it needs to match every single letter. So that means maybe you have an E there. Well, notice that it no longer matches because there is no E at the end of the word. That's important to remember. Everything must match unless you explicitly declare that it doesn't have to be matched, and we'll get into that a little bit later. So now, if I open up the substitution panel, we will update this to amazing. And that does work. However, let's add one more occurrence of stupid. This is so stupid. Well, notice that this matches too. However, that's not the default. That's only working because we've added this G flag here. So when you're writing your own regular expressions, that will not be enabled, in which case we will only update the first occurrence.

Using Alternation Matches1:34

That's only working because we've added this G flag here. So when you're writing your own regular expressions, that will not be enabled, in which case we will only update the first occurrence. So remember, whenever you want to say, I want to look through this entire paragraph or string and replace all occurrences of stupid with amazing, you need to provide the G flag for global. Alright, so this is looking good, but we forgot about idiotic and crap. We've decided that these three words alone are the only ones that we need to filter out of any comments. So what could we do here? Well, it's really very simple. If we know the exact words that we want to filter out, we could say, look for stupid, or look for idiotic, or look for crap. Now, you may remember that in a previous episode, we wrapped this kind of stuff within parentheses. And that would be essential, but only if our regular expression was more complicated. In this case, these are

that in a previous episode, we wrapped this kind of stuff within parentheses. And that would be essential, but only if our regular expression was more complicated. In this case, these are the only things we're searching for, so we don't have to worry about anything else. I'm saying, look for stupid, or look for idiotic, or look for the word crap. And of course, we provide the G flag to look for every occurrence of that set of characters. But now what about this? What if they finish off their comment by saying, you're an idiot? Hmm, well, now that didn't capture because we looked for the word idiotic rather than idiot. So what can we do in those cases? Well, you might think, well, let's just remove IC, and now we do match both of those. However, that's not quite right. Now, yes, we didn't match this first occurrence. However, we still get the IC at the end because we didn't

Making Sequences Optional3:05

remove IC, and now we do match both of those. However, that's not quite right. Now, yes, we didn't match this first occurrence. However, we still get the IC at the end because we didn't capture that. So instead, how can we say we want to look for idiot or idiotic? Well, you could always just be explicit by writing it out again. However, that's not exactly working like we expected. We wanted to match that, but it looks like nothing changed. And that's because, well, we had idiot before it. So we match this part and performed our replacement. That means this section never even got a chance to run, essentially. So that's not what we want. And besides, that's redundant. We don't want to do that. So maybe we could say look for idiot, but the IC, those two characters, those are optional. Now, you may remember from the previous episode, we talked about the question mark being a way to specify that the previous character or set is optional.

characters, those are optional. Now, you may remember from the previous episode, we talked about the question mark being a way to specify that the previous character or set is optional. And yes, that does seem to do the trick for this first replacement. But now we're back here, and the second one isn't working. Well, the reason for that is because all we said here is look for an I, D, I, O, T, I, and then maybe a C, but it's not required. So if you want to specify that this previous sequence, whatever I specify here is optional, that's when we can wrap within parentheses. So now we're saying this previous set, the I and the C together, those are optional. And now, once again, we're matching them. So this is working. But also, like we talked about in the previous episode, because we wrapped this within parentheses, we now have a matched group, which means we could access it in our substitution area using $1. And just to make sure that we're

Non-Capturing Groups4:43

previous episode, because we wrapped this within parentheses, we now have a matched group, which means we could access it in our substitution area using $1. And just to make sure that we're all on the same page, if I hover over this, notice group number one right there? It matched the I, C because we wrapped it within parentheses. So every time you place something within a group, you can then access it with $1, $2, $3. However, if you're ever in these situations where you're using parentheses less for the ability to hook onto it later and more for the simple purpose of grouping a sequence of characters together, you can always precede it with ? :. Now notice that creates a non-capturing group. That means we have a group here, but I don't want you to actually capture it so that I can use it later. I'm not interested in that at all. So now if I come back and we hover over this, notice we no longer have a group.

Applying Regex in PHP5:30

but I don't want you to actually capture it so that I can use it later. I'm not interested in that at all. So now if I come back and we hover over this, notice we no longer have a group. And as a result, $1 is now equal to nothing. So the important thing to remember is this is an option. In many cases, you're not even concerned about it enough, in which case this would be fine. However, it's really important to know that this is an option if you're ever in a situation where it's creating a new group and you don't really want that. So now that we've done our first very simple regular expression, I want to translate this to PHP so that you can see how to use it. So for this purpose, I will use a very simple editor called Code Runner, but it offers a nice feature to immediately execute PHP within the editor. I will say string equals and paste that in. Next, we will leverage preg_replace. The first argument is, what is your regular expression?

a nice feature to immediately execute PHP within the editor. I will say string equals and paste that in. Next, we will leverage preg_replace. The first argument is, what is your regular expression? So I will grab this, but just note that when I do copy it, it doesn't include the delimiters or the flags. So I will add those on myself. Now, the convention is to use forward slashes, but you're not required to. Just make sure that you are consistent. So if you want to use something like a pound sign, well, that's fine. Like I said, just be consistent on this, and as a rule of thumb, stick with the defaults, unless you have a reason not to. Next, what are we replacing this with? Well, we want to replace any occurrence of those bad words with amazing. And finally, what are we working with? string. So now, why don't we echo that out, and I can press Command R to run this within Code Runner. And that's it. So why don't we try this out. But before we do, you might be

working with? String. So now, why don't we echo that out, and I can press Command R to run this within Code Runner. And that's it. So why don't we try this out. But before we do, you might be wondering about the G flag, right? Well, if I press Command R in Code Runner to execute this, you'll find that PHP doesn't know anything about a G flag. And this can be confusing sometimes. Different languages will have different levels of support for regular expressions. Now, as it turns out in PHP, well, when you run preg_replace, the G will be implicit, which means it would be redundant to try to include it, and PHP won't even recognize it to begin with. So if I refresh this, notice it just works. Now, for other things like preg_match, well, if you want to simulate preg_match_all, well, there's a dedicated function for that. So I know that's a little tricky, but it's just something to memorize. So that means you could update your string, and then finally save the

Handling Case Insensitivity7:50

all, well, there's a dedicated function for that. So I know that's a little tricky, but it's just something to memorize. So that means you could update your string, and then finally save the fictional comment to the database, with the assurance that we have filtered out any bad words that we don't want to accept. Now though, just one last thing and then we'll be done. What if at the very end they use capital letters? Because people get very angry, and they have no choice but to use capital letters to express themselves. Well, now that one got through. So don't forget, we can use the ignore case flag, which means I don't care if it's a capital S or a lowercase s, just match something that's an S. So if we want to update this, refresh, and I'm sorry, echo out the string. Okay, well, now that's not matching. We add the flag on, like we did before, refresh, and now that one's working as well. Alright, so in this

refresh, and I'm sorry, echo out the string. Okay, well, now that's not matching. We add the flag on, like we did before, refresh, and now that one's working as well. Alright, so in this second episode, we talked a little bit more about optional characters, as well as how to specify optional sequences of characters. You learned more about non-capturing groups. You learned about alternation, where we can say match this or that or that. And also, we learned a little bit about using this stuff within the context of PHP. So if you feel comfortable with all of this, why don't we move on to the next episode?

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟