PHP 8 String Helpers0:00
Next up are three string-related helper functions. I recently joked that it took a global pandemic for php to finally add a string_contains function, but at least it's here, and that's a good thing. So let's get started. Let's say you have some kind of $id that's coming from an API request. It's a string, but in certain cases it begins with ch, which represents a charge. In other cases, inv for an invoice, maybe sub for a subscription. And basically, you need to inspect this to figure out what type of $id it is. Does it represent an invoice, or a charge, or a subscription, or a payment? Sometimes you have to do things like this. All right, well, traditionally, with vanilla php,
Checking String Prefix0:44
Sometimes you have to do things like this. All right, well, traditionally, with vanilla php, you're probably referring to those docs every time because it's easy to forget. You might say, well, I'm looking for ID, and we're going to check to see if it begins with inv. So if the position is zero, then we know it's an invoice. So let's just var_dump result, give it a run, and of course that returns true. But if it were something else, it's going to return false. Okay, well, in PHP 8, it's a little bit cleaner. We can just change it to something a bit more descriptive. If the ID begins with inv, then we know it's an invoice, and it passes.
Checking String Suffix1:18
We can just change it to something a bit more descriptive. If the ID begins with inv, then we know it's an invoice, and it passes. Of course, in PHP 7, it'll fail. All right, let's do another one. Let's stick with that same ID, but maybe in this case, the type is actually at the end of the string. So it'll take the shape of something like this, or charge, or payment. You get the idea. Let's do charge this time. Okay, you're going to do string pos again, but you're looking for this, but it's not overly useful because it's going to give you the character string, so 14 in this case, and you don't really want to say this because that's not quite what you want.
but it's not overly useful because it's going to give you the character string, so 14 in this case, and you don't really want to say this because that's not quite what you want. So yeah, you probably end up doing something like, well, let's just reverse the string. That would then turn this into H-C-A-S-D-S. You get the idea. So that's just kind of a quick way to deal with this. Reverse it, and now check to see if that position is zero, and that would be one way to check if it ends. In this case, it does, but if we bring it back, that would return false. That's the traditional way, or, I don't love this, often even if it's a little bit slower, I would reach for preg_match, so that would be something like this, preg_match.
That's the traditional way, or, I don't love this, often even if it's a little bit slower, I would reach for preg_match, so that would be something like this, preg_match. I'm going to look for underscore C-H and then the end of the string, and compare that against the I-D. So that would return one for true, like so, but if it ended in something else, that'll return false. So yeah, often I will reach for regular expressions. It ends up being a little cleaner in my mind, but yeah, you don't have to deal with this at all anymore. In PHP 8, just check, does the string end with the given I-D? What do we have here? Invoice, in this case.
Checking String Contains3:10
In PHP 8, just check, does the string end with the given ID? What do we have here? Invoice, in this case. We give it a run, and of course, that's going to pass, but if we change it back, that's going to fail. Once again, in PHP 7, this blows up as well. Okay, finally, string contains. In the last one, maybe you have a URI or L. So maybe you need to check if we have a query string here. Now, of course, you can just defer to parse_url, give that a run, and yeah, we can see there is a query there, so of course you can just check that,
Now, of course, you can just defer to parse_url, give that a run, and yeah, we can see there is a query there, so of course you can just check that, and sure enough, we do have a query string. But if you didn't have one, that would return undefined array key. Maybe one way to do it, parse_url, but assuming this isn't a URL, or it's just an example, in the past, once again, you would check to see if strpos, look at the URL, look for a question mark, and make sure it does not equal false. So if we give that a run, it returns true, of course. But if we don't have a query string, give it a run, that will fail.
So if we give that a run, it returns true, of course. But if we don't have a query string, give it a run, that will fail. Okay, in php 8, just reach for str_contains, look at the URL, and check to see if we have a question mark. Run it, that fails, but if we add a question mark, or a query string, run it, now that passes. Very useful.
