PHP-and-MySQL/C4/PHP-String-Functions-Part-2/English

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

Contributors and Content Editors

Chandrika, Pravin1389