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