PHP String Functions8:59 with Hampton Paulk
Strings are a major data type for us in PHP and something we will often need to search, parse, & test against. Let's look at some of the most common of the PHP's built-in string functions.
Strings are a major data type for us in PHP and 0:00 something we'll need to search, parse, and test against very often. 0:03 Let's look at some of the most common of PHP's built in string functions. 0:07 The last function that we were looking at is string length. 0:13 We were looking at it just to see how we actually utilize string length. 0:16 Now, let's go ahead over to Workspaces. 0:20 I'm gonna toggle over and actually use string length. 0:23 We're gonna look at a few different string functions. 0:27 So, mainly they're meant to manipulate or look into strings. 0:30 They might return something other than a string such as, 0:34 string length is going to return integer. 0:37 However, we want to work with strings. 0:39 So, we'll use string functions. 0:42 Let's start by defining a string. 0:44 We will call it phrase. 0:47 And, that phrase will be we only hit what we aim for. 0:51 All right. 0:59 Going to end that with a semi-colon and hit save. 1:00 Now, what we want to do is look at our string length doc. 1:02 So, it's gonna return to us an integer value of how long a string is. 1:06 So, we're going to call string length. 1:10 So, we'll say, str len, right? 1:13 Let's just make sure that's correct. 1:15 Str len, right. 1:18 And then, our argument that we pass through, if you look here on the docs, 1:20 is a string. 1:24 So, we have our string, which is phrase, so we'll pass through phrase. 1:25 Okay. And then, end it with a semicolon. 1:31 Now, this won't output anything, but it will run the string length, so 1:33 we'll actually assign that to a variable and call it len. 1:36 All right. 1:40 Now, all we have to do is echo that to our screen. 1:41 So, echo and then len. 1:44 Okay. Let's switch over and preview this. 1:49 Okay. And, we get 27. 1:52 So, we have a total of 27 characters in length, in our string. 1:53 Pretty cool. 1:57 Okay. So next, 2:01 we're going to take a look at two different string functions. 2:02 One of them is gonna be sub-string, S-U-B-S-T-R, and 2:06 the other one is going to be string pause or string position. 2:11 So, S-T-R-P-O-S. 2:17 So, let's go over and open up a page for 2:19 the docs and find sub-string and string position. 2:21 So, we'll go ahead and copy that. 2:25 I'll create a new one here. 2:27 And then, I'm gonna paste in sub-string, and it says return part of a string. 2:29 So, you'll see here that in the definition or 2:34 in the description, we're going to return part of a string. 2:37 It's going to return to us a string value. 2:39 It's expecting 2 arguments. 2:43 This first argument is string or whatever string we want to look through. 2:45 The second is a integer value from where it wants to start. 2:49 So, we can start at the first position or so 2:53 many positions by character down the string. 2:55 Then, if you notice in the square brackets, that means it's optional. 3:00 But, we can pass through an optional link. 3:03 So, let's go ahead and play with this now inside of WorkSpaces. 3:06 So, we'll do that by go ahead and using the keyword or the function sub-string. 3:12 So, S-U-B-S-T-R. 3:17 Then, we're going to pass through the first argument which is the actual phrase. 3:20 Okay. And then, 3:26 the second one is where we want to start. 3:27 So, we want to start from a zero based position, so we'll hit zero. 3:29 And then, we'll go ahead and close this, and then hit Save. 3:33 And, that's going to start from position one, and 3:38 it's going to return the entire string. 3:40 So, lets actually echo that out, directly and see what we get back. 3:43 Just wanna switch back over to our preview ,and then we 3:47 see we only hit what we aim for. 3:50 Now, that's just returning the whole string. 3:53 But, what if we wanted to start from say, so many characters in? 3:55 Let's head back over, and we're going to pass through a second argument, or 3:58 our first argument we're going to change. 4:02 Instead of zero, or the first position, we'll change it to position five. 4:03 We'll hit save, and go back over and refresh. 4:07 So now, it says, LY hit what we aim for, because we started at position five. 4:11 Now, what if we started at position 0, but 4:16 we only wanted to get the first five characters? 4:19 Well, we can do that too just by simply passing our third or 4:21 optional argument, which would be in this case, the numeric 5. 4:25 Switch back over and refresh. 4:28 And now, it says, We on. 4:31 Now, at first glance, you might think, okay, this is only four characters. 4:33 But, there is a space in between the we and 4:36 the o, n, so that actually counts as a character. 4:39 So, a space is technically a character for us. 4:42 So, we and then space, o, n, is a total of five characters. 4:46 The next one we wanna look at is string position. 4:51 So, S-T-R-P-O-S. 4:53 Let's switch back over and look at the manual and 4:54 we'll go to the search S-T-R-P-O-S. 4:58 Here it is. 5:01 So, this is going to find the first position or 5:02 the position of the first occurrence of a sub-string inside of a string. 5:06 So, they've used the keywords here, or the, the array names to make a little bit 5:11 of sense in the description that says needle and haystack. 5:15 So, there's a phrase called looking for a needle in a haystack. 5:19 Well, our haystack is our full string, and the needle is how we, 5:22 what we want to find. 5:26 So, it's going to be a particular occurrence, 5:28 using the needle inside of the haystack. 5:31 So, let's take a look at it in code, and see what we get back. 5:33 Okay? 5:37 So, here we're gonna switch back over to workspaces, and 5:38 we're going to actually comment out this line, just for any confusion. 5:41 And then, go echo, and then S-T-R-P-O-S. 5:45 And then, our haystack, which is the first thing that goes through, is our phrase. 5:50 Then, if you look back over at our documentation, 5:55 the needle is also required. 5:59 So, we will actually have to pass through a needle. 6:00 Now, in this case, if you scroll down here. 6:03 It says if the needle is not a string, it's then converted to an integer and 6:06 applied as the ordinal value of a character. 6:11 So, it's expecting that you would put through a string normally. 6:14 So, you can put through an integer but 6:19 in this case, we're gonna put through a string. 6:21 So, we're going to put through hit as a string. 6:23 So, quote H-I-T, and another single quote, and then hit semi-colon. 6:25 So, we'll save that and switch back over to our preview. 6:31 Okay. So, it returned to 6:35 us a integer value of eight. 6:36 So, it's at position eight. 6:39 Let's look at the docs and see what it means by position. 6:40 So, it's going to return to us a string position, okay? 6:44 So, it is a string position starting at zero, not one. 6:48 So, if you notice here it says, 6:53 also note that string positions start at zero and not one. 6:55 So, for us, when it says eight, that means that the w on line three is actually zero. 6:59 So, if we count all the way out to the h, 7:05 the h arrives at integer eight in a zero-based index. 7:07 So, that's how we find it. 7:12 Now, if we were to, say change this to Bob, and hit save, and 7:13 then we go back and refresh. 7:19 What we're gonna get is nothing, but if we were to var_dump this instead of 7:21 printing it or instead of echoing it to the screen, let's see what we get. 7:27 We get a Boolean false. 7:33 If we look at our manual here, we'll actually see that it 7:34 returns false if the needle was not found, which is exactly what we were expecting. 7:38 So, we can search for 7:42 a string inside of another string, and get the position that it starts from. 7:44 And then, return the string starting at that point or 7:49 do any kind of other functions that we need with this. 7:52 So, we can actually combine the two functions we just used. 7:55 So, let's do that now. 7:58 So, here we have echo sub-string, which is fine, we're gonna continue to use that, 8:00 but I'm actually gonna move it below here. 8:04 And then, I'm going to do a string position, but 8:07 I'm going to sign it to a variable called start. 8:09 And then, I'm going to search for 8:13 a particular phrase, which in our case is going to be hit, right? 8:15 And then, what I want to do is actually echo the word 8:20 hit until the end of the sentence. 8:24 So in substring I remove my final argument. 8:27 And, instead of passing an integer to line 15 as the second argument, 8:30 I'll pass through my variable, which is an integer value called start. 8:34 Let's save that and go back over to our page and refresh, and see what we get. 8:39 So now, we get, hit what we aim for, which is exactly what I was looking for. 8:45 So, you can see by combining two different string functions in one little bit 8:49 of code we can actually get some very powerful, powerful results. 8:54
You need to sign up for Treehouse in order to download course files.Sign up