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

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

Contributors and Content Editors

Gyan, Pratik kamble, Sandhya.np14