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