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