PHP-and-MySQL/C4/PHP-String-Functions-Part-1/English
From Script | Spoken-Tutorial
Time | Narration |
---|---|
0:00 | Welcome to the tutorial on string functions. |
0:03 | I'll take you through the string functions that are shown here. |
0:06 | Most of them are very useful and applicable to everyday applications. |
0:10 | And also definitely applicable to all the videos I will or have really made. |
0:16 | Ok,so... the first one I'll show you is "strlen". |
0:20 | This is extremely simple in which we have a string value of lets say "hello". |
0:26 | Now, this function takes a string and counts the number of characters in that string. |
0:30 | So here we've got 1 2 3 4 5 characters. |
0:35 | And if we were to echo out the value of the variable string using this function, we should have a result of 5 in our browser. |
0:47 | Now, the next function is applicable to this. |
0:52 | If you were looping through the number of string characters using a 'for' loop, you would need mb substring to take a specific subsstring inside. |
1:03 | So, for example if you had the string of, lets say, "My name is Alex", |
1:12 | And we wanted to loop through this, checking each character as we went... |
1:18 | For eg- if you check out my 'Name Splitter' tutorial, we loop through each single character until we find a space and then from there on we save the last name. |
1:32 | So first, I'll echo out mb substring. |
1:37 | And next we will specify the string that we need to check. |
1:40 | You need to specify the starting point so I'll say 1. |
1:45 | In fact, I'll say zero and then the length - I'll say 2. |
1:49 | And this should echo out "My". |
1:52 | Refresh. Ok. We've got "My" there. |
1:57 | So what it's done is it's gone through this string and said, "Right, we'll start at zero, and we'll say, for 1, 2, we'll echo out that here. |
2:05 | Now what I will do, is say, I will say 's-t-r-len', oh sorry, length equals strlen of string. |
2:15 | I'm creating a new variable here of the length of this string. |
2:19 | And then I'll replace 2 with this value. |
2:22 | As long as I start from zero, I can put the string length in there or rather sorry, length in there and as we refresh we get the whole of the string. |
2:37 | And what I could also do, is say s-t-r-len minus 5 on the end here for my name including the full stop - so I'm saying minus 5. |
2:49 | So that would take five off the length and just echo out "My name is" |
2:53 | Refresh and we get 'My name is'. |
2:56 | So these two functions are quite versatile and using the strlen which applies to mb substring here. |
3:03 | Ok. So the next function I'll go through is 'explode'. |
3:07 | Now explode will take a string as we have here |
3:13 | Lets just say 1 2 3 4 5 |
3:17 | And the explode function it will echo out explode. |
3:23 | It will break your string, a plain string. From the start to the finish, it will break it down into an array. |
3:32 | So say we want to create and write out this. |
3:35 | I want 1 2 3 4 5 stored in each separate element of the array. |
3:40 | I'll say explode string. No I'll not - I'll specify what is used to break up the string. |
3:45 | At the moment its the space. |
3:49 | If we had slash, we would replace it by slash. |
3:51 | Because this would be the determinant where it starts
and this is the separator. |
3:57 | This is the second value.
So for the moment we will have space. Ok? |
4:03 | So you can add anything you want in here. It can be an asterisk. |
4;06 | It can be any symbol really. You just need to specify what breaks up the string. |
4:11 | Explode and then the name of the string. |
4:16 | And that should be it. |
4:18 | Lets check that out. |
4:20 | Refresh. |
4:22 | Array. At the moment echoing out the array. |
4:26 | Obviously you can see that what I have done is just echoed out an array. |
4:30 | We can say it has been set to an array because we've learnt that in my Array tutorial, my basic tutorial. |
4:35 | And here it tells us we have an array. |
4:37 | So now if we just use this function on the same and then echo out ... |
4:41 | In fact, we need to first set this to a variable. |
4:44 | So lets say exp - array equals that and then were going to say exp - array and we can echo out numbers. |
4:52 | We can use zero, one, two, three, four and that would be it. |
4:56 | So as soon as this value is zero, this would equal 1. |
5:01 | So lets say I want to echo out 1 that should equal 2. |
5:06 | Okay, so we've successfully broken our array. |
5:09 | Like I said before we put slashes here and just replace space with slash. |
5:16 | And we'll have exactly the same result here. |
5:21 | Ok? So that's 'explode'. |
5:23 | Now the opposite of that is 'implode'. |
5:26 | Let me get rid of this. |
5:28 | And you can see the 'implode' function here, also called join. |
5:32 | So you can call it as join or implode whatever you prefer. |
5:38 | So what I'll do is type a new string and its the value of implode and what we're going to implode is our 'exparray'. |
5:51 | Ok so lets try it out. |
5:55 | Ok we've done that without any errors. |
5:57 | Now if we echo out our new string - |
6:01 | That should recall what we started with before, with no spaces. |
6:05 | But what can be done is you can specify with what to break up your array. |
6:09 | So here I decided to add space. But if you wanted slash there you could just put a forward slash here and get that result. |
6:21 | But coming back these functions are for converting 'to' and 'from' arrays. |
6:27 | So explode and implode. And like we said before this can written as join. |
6:32 | So refresh and we get exactly the same result. |
6:34 | So that's the 'implode' function there. |
6:36 | Ok - the next function we go to is nl2br. |
6:41 | Now this function is really functional and easy when we are working with data bases. |
6:46 | When data is being stored on an immediate-line-basis. |
6:51 | Now remember I said that if you had watched my basic tutorials you would've known that... |
6:58 | This 'Hello' or should I say 'Hello', 'New line', 'Another new line' and I'll put the semi-colon that is the line break here |
7:12 | Lets keep it as that. |
7:16 | Ok so if I echo this out, we can imagine what will happen. |
7:19 | We will get this. |
7:21 | If we want them on separate lines then we should use 'br'. |
7:30 | So if you don't want to use html for some reason or if you are taking from data base result, you will have to build quite a complex function to put line breaks in it. |
7:44 | It happens when people set me in to a data base. |
7:47 | So if you cannot build and just have this lame test in the database, what you really want to do is just echo out without having to manually use quotes and put breaks in it- if that makes sense. |
7:59 | But if you put nl2br at the start of a string and we'll just end the bracket there |
8:04 | You'll see it will echo out the exact way we want it to. |
8:08 | We'll get a line break at the top because we have done this - added a space here. Let's remove that. |
8:16 | So without nl2br we would get everything in one line and with nl2br we would get separate lines, just the way we want it |
8:30 | Ok so I'll stop this video here as I'm running out of time. There's a second part for the rest of these functions. Be sure to catch it. |
8:38 | See you soon. This is Osama Butt dubbing for the Spoken Tutorial Project. |