Ok back to the second part of the String Functions tutorial.
I'm going to go through the rest of the functions starting from String Reverse.
String reverse probably to make sense is s-t-r-rev.
So what strvev does is it reverses the contents of a string.
So if I were to say 'Hello' and I were to reverse that, it would be "o-l-l-e-H".
And it can be useful in some circumstances although you usually wouldn't use this.
But you could use this function if you want to specifically reverse a string.
I think its a useful and fun function to use.
Ok - the next set of functions I have grouped together are these: str to lower and str to upper.
This basically means string to lower case and string to upper case.
So if we have our string that equals 'HELLO', I can say echo str to lower and show the value of the string in there.
The 'HELLO' in capitals will now become lowercase.
Something similar would happen if I were to say this is 'hello' in smallcase
And I could say str to upper and that would give me my uppercase version of the string.
Now one applicable use of this is when you have user-registration.
If you have a website on which users have to register, you should usually always store the user name as a lower string.
The reason is that if I submit a user name - lets get rid of this...
Some people actually do this - Lets have a variable user name which equals say 'ALEX'.
And I'll put in these also - uppercase and smallcase alphabets.
Some people use names like this to make the name look funky and its perfectly okay.
But if the name is stored as this and you think - well did i start with a small a?
Then I have another pattern for username now.
So what you can do is say stored user name equals to str to lower of the username.
So this would be the stored username in the database
Now when they go to login and type in their username in this combination, what we would do is we would convert their typed-in login username to lower case and compare it to the lower case store version of the username.
So we are taking this and storing a lowercase value inside the database and we are comparing it to a typed-in value which has also been converted to lower case
Hence we can't go wrong and users are not going to forget their user-names.
You could do the same with passwords.
Ok lets go to the next one.
Sub-string count. This would basically count the no. of sub-strings matching to a particular value inside a string.
So here I'll type search equals "My name is alex. What is your name?"
So this is our string.
Now if I say we need to echo out the sub-string count...
and obviously this stands for sub-string-count, what we want to do is, we want to search our 'search' string...
and we will specify a string to search for. Now this will return an integer if we put this in a variable called result.
That's because you can't find any instance of a word which will appear say for 1.2 times.
Also the variable result will not return 2 as t-w-o. It will only return 2 as an integer.
So this is quite useful if we are using substring count to search for, lets say, 'alex'.
And then it will echo out on its own.
And if you look through here, you will see there is only one instance of 'alex'
So refresh that - and we should get the number 1.
Now if we were to search for 'name' - there's 1 instance of 'name' here and another instance of 'name' here
So when we refresh, we should get the value 2.
Now there are optional parameters for this, which are 'where to start from in a string' and 'where to end in a string'.
Lets try this out.
So lets say I want to search from after name, ok?
So this is 0 1 2 3 4 5 6.
So I say search name from 7 onwards.
So search name from 7 and it will search in this blue area that I have highlighted here.
It will only return 1 in the result.
So you can specify whereabouts in the string.
I think you can specify upto where.
So this is 7... 8 9 10 11 12 13 14 15 16.
7 to 17. Lets check if this works.
It shows zero. So from 7 to 17 - which is from about here to here - we find zero instances of 'name'.
However if we search for 'alex', we will find 1 instance of it.
Ok - so that's the substring count function.
And now substring replace is similar.
Its not the same function but it includes an added bonus where you can replace your string.
So the replace tags are - My name is alex and I've added the full-stop on purpose.
Our result is equal to substring replace.
What do I want to replace in? I want to replace in the variable replace.
And I want to replace 'alex' with 'billy'.
And this will be from - let me count 0 1 2 3 4 5 7 8 9 10 11 so from 11 until...
Its 11 - 0 1 2 3 4 5 6 7 8 9 10 11 - from 11 to 14.
So that should replace 'alex' with 'billy'.
Replace and refresh.
Oh! We didn't echo out result.
Lets echo out result and we can refresh this.
And it should return my name is billy.
This should be 12 and this 15, I think.
Or in fact no - it should be 10 and 14.
No, not quite right.... We are missing the full-stop.
........ so lets go for 11 and 14.
Still missing the full stop and I cant think why.
aah! well you get the picture
Basically you can replace anything in the string with the starting value and the ending value
It will leave it up to you to count through.
I'm very tired so I am not able to count.
So what we are doing here is we are replacing a particular string with a particular value
And here's your starting value and here's your finishing value.
That's all in this tutorial.
There are many more string functions and I suggest you to search, may be, on 'google'.
Search for 'php string functions' and you'll find a lot of interesting functions.
If you are looking to do a particular thing there's probably a function available for it.
Thanks for watching! This is Osama Butt dubbing for the Spoken Tutorial Project.