1 00:00:00,490 --> 00:00:03,340 Strings are a major data type for us in PHP and 2 00:00:03,340 --> 00:00:07,740 something we'll need to search, parse, and test against very often. 3 00:00:07,740 --> 00:00:11,740 Let's look at some of the most common of PHP's built in string functions. 4 00:00:13,170 --> 00:00:16,070 The last function that we were looking at is strlen. 5 00:00:16,070 --> 00:00:20,650 We were looking at it just to see how we actually utilize strlen. 6 00:00:20,650 --> 00:00:23,100 Now, let's go ahead over to Workspaces. 7 00:00:23,100 --> 00:00:27,180 I'm gonna toggle over and actually use strlen. 8 00:00:27,180 --> 00:00:30,530 We're gonna look at a few different string functions. 9 00:00:30,530 --> 00:00:34,840 So, mainly they're meant to manipulate or look into strings. 10 00:00:34,840 --> 00:00:37,380 They might return something other than a string such as, 11 00:00:37,380 --> 00:00:39,690 strlen is going to return integer. 12 00:00:39,690 --> 00:00:42,210 However, we want to work with strings. 13 00:00:42,210 --> 00:00:44,450 So, we'll use string functions. 14 00:00:44,450 --> 00:00:47,350 Let's start by defining a string. 15 00:00:47,350 --> 00:00:49,130 We will call it phrase. 16 00:00:51,950 --> 00:00:59,510 And, that phrase will be we only hit what we aim for. 17 00:00:59,510 --> 00:01:00,500 All right. 18 00:01:00,500 --> 00:01:02,820 Going to end that with a semi-colon and hit save. 19 00:01:02,820 --> 00:01:06,370 Now, what we want to do is look at our strlen doc. 20 00:01:06,370 --> 00:01:10,970 So, it's gonna return to us an integer value of how long a string is. 21 00:01:10,970 --> 00:01:13,070 So, we're going to call strlen. 22 00:01:13,070 --> 00:01:15,099 So, we'll say, strlen, right? 23 00:01:15,099 --> 00:01:18,777 Let's just make sure that's correct. 24 00:01:18,777 --> 00:01:20,500 strlen, right. 25 00:01:20,500 --> 00:01:24,080 And then, our argument that we pass through, if you look here on the docs, 26 00:01:24,080 --> 00:01:25,430 is a string. 27 00:01:25,430 --> 00:01:29,230 So, we have our string, which is phrase, so we'll pass through phrase. 28 00:01:31,000 --> 00:01:33,240 Okay. And then, end it with a semicolon. 29 00:01:33,240 --> 00:01:36,880 Now, this won't output anything, but it will run the string length, so 30 00:01:36,880 --> 00:01:40,430 we'll actually assign that to a variable and call it $len. 31 00:01:40,430 --> 00:01:41,310 All right. 32 00:01:41,310 --> 00:01:44,220 Now, all we have to do is echo that to our screen. 33 00:01:44,220 --> 00:01:49,240 So, echo and then len. 34 00:01:49,240 --> 00:01:52,160 Okay. Let's switch over and preview this. 35 00:01:52,160 --> 00:01:53,490 Okay. And, we get 27. 36 00:01:53,490 --> 00:01:57,960 So, we have a total of 27 characters in length, in our string. 37 00:01:57,960 --> 00:01:58,460 Pretty cool. 38 00:02:01,340 --> 00:02:02,120 Okay. So next, 39 00:02:02,120 --> 00:02:06,140 we're going to take a look at two different string functions. 40 00:02:06,140 --> 00:02:11,140 One of them is gonna be sub-string, S-U-B-S-T-R, and 41 00:02:11,140 --> 00:02:15,950 the other one is going to be strpos or string position. 42 00:02:17,370 --> 00:02:19,300 So, S-T-R-P-O-S. 43 00:02:19,300 --> 00:02:21,860 So, let's go over and open up a page for 44 00:02:21,860 --> 00:02:25,220 the docs and find substr and strpos. 45 00:02:25,220 --> 00:02:27,390 So, we'll go ahead and copy that. 46 00:02:27,390 --> 00:02:29,480 I'll create a new one here. 47 00:02:29,480 --> 00:02:34,340 And then, I'm gonna paste in substr, and it says return part of a string. 48 00:02:34,340 --> 00:02:37,220 So, you'll see here that in the definition or 49 00:02:37,220 --> 00:02:39,720 in the description, we're going to return part of a string. 50 00:02:39,720 --> 00:02:43,150 It's going to return to us a string value. 51 00:02:43,150 --> 00:02:45,570 It's expecting 2 arguments. 52 00:02:45,570 --> 00:02:49,960 This first argument is string or whatever string we want to look through. 53 00:02:49,960 --> 00:02:53,770 The second is a integer value from where it wants to start. 54 00:02:53,770 --> 00:02:55,950 So, we can start at the first position or so 55 00:02:55,950 --> 00:03:00,040 many positions by character down the string. 56 00:03:00,040 --> 00:03:03,980 Then, if you notice in the square brackets, that means it's optional. 57 00:03:03,980 --> 00:03:06,730 But, we can pass through an optional length. 58 00:03:06,730 --> 00:03:09,970 So, let's go ahead and play with this now inside of WorkSpaces. 59 00:03:12,500 --> 00:03:17,650 So, we'll do that by go ahead and using the keyword or the function substr. 60 00:03:17,650 --> 00:03:20,310 So, S-U-B-S-T-R. 61 00:03:20,310 --> 00:03:25,020 Then, we're going to pass through the first argument which is the actual phrase. 62 00:03:26,590 --> 00:03:27,160 Okay. And then, 63 00:03:27,160 --> 00:03:29,090 the second one is where we want to start. 64 00:03:29,090 --> 00:03:33,290 So, we want to start from a zero based position, so we'll hit zero. 65 00:03:33,290 --> 00:03:38,130 And then, we'll go ahead and close this, and then hit Save. 66 00:03:38,130 --> 00:03:40,930 And, that's going to start from position one, and 67 00:03:40,930 --> 00:03:43,330 it's going to return the entire string. 68 00:03:43,330 --> 00:03:47,620 So, lets actually echo that out, directly and see what we get back. 69 00:03:47,620 --> 00:03:50,630 Just wanna switch back over to our preview, and then we 70 00:03:50,630 --> 00:03:53,210 see we only hit what we aim for. 71 00:03:53,210 --> 00:03:55,170 Now, that's just returning the whole string. 72 00:03:55,170 --> 00:03:58,920 But, what if we wanted to start from say, so many characters in? 73 00:03:58,920 --> 00:04:02,250 Let's head back over, and we're going to pass through a second argument, or 74 00:04:02,250 --> 00:04:03,610 our first argument we're going to change. 75 00:04:03,610 --> 00:04:07,810 Instead of zero, or the first position, we'll change it to position five. 76 00:04:07,810 --> 00:04:11,080 We'll hit save, and go back over and refresh. 77 00:04:11,080 --> 00:04:16,360 So now, it says, ly hit what we aim for, because we started at position five. 78 00:04:16,360 --> 00:04:19,400 Now, what if we started at position 0, but 79 00:04:19,400 --> 00:04:21,970 we only wanted to get the first five characters? 80 00:04:21,970 --> 00:04:25,220 Well, we can do that too just by simply passing our third or 81 00:04:25,220 --> 00:04:28,980 optional argument, which would be in this case, the numeric 5. 82 00:04:28,980 --> 00:04:31,120 Switch back over and refresh. 83 00:04:31,120 --> 00:04:33,250 And now, it says, We on. 84 00:04:33,250 --> 00:04:36,570 Now, at first glance, you might think, okay, this is only four characters. 85 00:04:36,570 --> 00:04:39,830 But, there is a space in between the we and 86 00:04:39,830 --> 00:04:42,840 the o, n, so that actually counts as a character. 87 00:04:42,840 --> 00:04:46,050 So, a space is technically a character for us. 88 00:04:46,050 --> 00:04:50,160 So, we and then space, o, n, is a total of five characters. 89 00:04:51,410 --> 00:04:53,750 The next one we wanna look at is string position. 90 00:04:53,750 --> 00:04:54,990 So, S-T-R-P-O-S. 91 00:04:54,990 --> 00:04:58,260 Let's switch back over and look at the manual and 92 00:04:58,260 --> 00:05:01,880 we'll go to the search S-T-R-P-O-S. 93 00:05:01,880 --> 00:05:02,940 Here it is. 94 00:05:02,940 --> 00:05:06,220 So, this is going to find the first position or 95 00:05:06,220 --> 00:05:11,600 the position of the first occurrence of a sub-string inside of a string. 96 00:05:11,600 --> 00:05:15,380 So, they've used the keywords here, or the, the array names to make a little bit 97 00:05:15,380 --> 00:05:19,290 of sense in the description that says needle and haystack. 98 00:05:19,290 --> 00:05:22,330 So, there's a phrase called looking for a needle in a haystack. 99 00:05:22,330 --> 00:05:26,530 Well, our haystack is our full string, and the needle is how we, 100 00:05:26,530 --> 00:05:28,130 what we want to find. 101 00:05:28,130 --> 00:05:31,240 So, it's going to be a particular occurrence, 102 00:05:31,240 --> 00:05:33,660 using the needle inside of the haystack. 103 00:05:33,660 --> 00:05:37,270 So, let's take a look at it in code, and see what we get back. 104 00:05:37,270 --> 00:05:38,450 Okay? 105 00:05:38,450 --> 00:05:41,160 So, here we're gonna switch back over to workspaces, and 106 00:05:41,160 --> 00:05:45,420 we're going to actually comment out this line, just for any confusion. 107 00:05:45,420 --> 00:05:50,300 And then, go echo, and then S-T-R-P-O-S. 108 00:05:50,300 --> 00:05:54,070 And then, our haystack, which is the first thing that goes through, is our phrase. 109 00:05:55,380 --> 00:05:59,000 Then, if you look back over at our documentation, 110 00:05:59,000 --> 00:06:00,690 the needle is also required. 111 00:06:00,690 --> 00:06:03,770 So, we will actually have to pass through a needle. 112 00:06:03,770 --> 00:06:06,730 Now, in this case, if you scroll down here. 113 00:06:06,730 --> 00:06:11,700 It says if the needle is not a string, it's then converted to an integer and 114 00:06:11,700 --> 00:06:14,960 applied as the ordinal value of a character. 115 00:06:14,960 --> 00:06:19,270 So, it's expecting that you would put through a string normally. 116 00:06:19,270 --> 00:06:21,100 So, you can put through an integer but 117 00:06:21,100 --> 00:06:23,050 in this case, we're gonna put through a string. 118 00:06:23,050 --> 00:06:25,890 So, we're going to put through hit as a string. 119 00:06:25,890 --> 00:06:31,800 So, quote H-I-T, and another single quote, and then hit semi-colon. 120 00:06:31,800 --> 00:06:35,840 So, we'll save that and switch back over to our preview. 121 00:06:35,840 --> 00:06:36,980 Okay. So, it returned to 122 00:06:36,980 --> 00:06:39,050 us a integer value of eight. 123 00:06:39,050 --> 00:06:40,730 So, it's at position eight. 124 00:06:40,730 --> 00:06:44,240 Let's look at the docs and see what it means by position. 125 00:06:44,240 --> 00:06:48,230 So, it's going to return to us a string position, okay? 126 00:06:48,230 --> 00:06:53,470 So, it is a string position starting at zero, not one. 127 00:06:53,470 --> 00:06:55,530 So, if you notice here it says, 128 00:06:55,530 --> 00:06:59,410 also note that string positions start at zero and not one. 129 00:06:59,410 --> 00:07:05,800 So, for us, when it says eight, that means that the w on line three is actually zero. 130 00:07:05,800 --> 00:07:07,580 So, if we count all the way out to the h, 131 00:07:07,580 --> 00:07:12,210 the h arrives at integer eight in a zero-based index. 132 00:07:12,210 --> 00:07:13,610 So, that's how we find it. 133 00:07:13,610 --> 00:07:19,280 Now, if we were to, say change this to Bob, and hit save, and 134 00:07:19,280 --> 00:07:21,650 then we go back and refresh. 135 00:07:21,650 --> 00:07:27,860 What we're gonna get is nothing, but if we were to var_dump this instead of 136 00:07:27,860 --> 00:07:31,760 printing it or instead of echoing it to the screen, let's see what we get. 137 00:07:33,230 --> 00:07:34,620 We get a Boolean false. 138 00:07:34,620 --> 00:07:38,190 If we look at our manual here, we'll actually see that it 139 00:07:38,190 --> 00:07:42,880 returns false if the needle was not found, which is exactly what we were expecting. 140 00:07:42,880 --> 00:07:44,000 So, we can search for 141 00:07:44,000 --> 00:07:49,380 a string inside of another string, and get the position that it starts from. 142 00:07:49,380 --> 00:07:52,520 And then, return the string starting at that point or 143 00:07:52,520 --> 00:07:55,090 do any kind of other functions that we need with this. 144 00:07:55,090 --> 00:07:58,010 So, we can actually combine the two functions we just used. 145 00:07:58,010 --> 00:07:58,860 So, let's do that now. 146 00:08:00,460 --> 00:08:04,920 So, here we have echo substr, which is fine, we're gonna continue to use that, 147 00:08:04,920 --> 00:08:07,390 but I'm actually gonna move it below here. 148 00:08:07,390 --> 00:08:09,760 And then, I'm going to do a string position, but 149 00:08:09,760 --> 00:08:13,240 I'm going to assign it to a variable called start. 150 00:08:13,240 --> 00:08:15,160 And then, I'm going to search for 151 00:08:15,160 --> 00:08:20,190 a particular phrase, which in our case is going to be hit, right? 152 00:08:20,190 --> 00:08:24,530 And then, what I want to do is actually echo the word 153 00:08:24,530 --> 00:08:27,490 hit until the end of the sentence. 154 00:08:27,490 --> 00:08:30,980 So in substring I remove my final argument. 155 00:08:30,980 --> 00:08:34,740 And, instead of passing an integer to line 15 as the second argument, 156 00:08:34,740 --> 00:08:38,490 I'll pass through my variable, which is an integer value called start. 157 00:08:39,490 --> 00:08:43,940 Let's save that and go back over to our page and refresh, and see what we get. 158 00:08:45,500 --> 00:08:49,870 So now, we get, hit what we aim for, which is exactly what I was looking for. 159 00:08:49,870 --> 00:08:54,780 So, you can see by combining two different string functions in one little bit 160 00:08:54,780 --> 00:08:58,590 of code we can actually get some very powerful, powerful results.