String Manipulation Functions

Hey guys, welcome back in this video, we will be looking at string manipulations with PHP. Now as usual, the first thing we do is to create a link in our index page that will speak to our string manipulation. So I’m just going to duplicate that. And I’m just call it string manip dot php, and just rename all this text. Now that I have the link up, I need to create the page that this will navigate to. So I go over and create a new document called it string manip dot php. And then I go ahead and bring up my html5 boilerplate. So after doing all that and changing my title, let’s get straight into the fun. So firstly, we’re doing PHP, so we have to use our PHP tags. And then I’m going to declare two variables here. And remember that to declare a variable we just need $1 sign and then we give it a name. So we See, freeze one is going to be equal to, and I want it to be a string. And this time, I’m going to use double quotation marks. And I’m going to say, A, let’s say, students who came bait. Alright. And then I’m going to declare another variable, and I’m going to call it phrase two. And then this one is going to see in class, comma stand with rock. Alright, two random sentences, they don’t really mean anything. But essentially, by now, we should know how to declare variables and assign values to them. And we know how to echo variables to our screen. So if I wanted to print out what is being expressed by these phrases, I can just say echo, freeze, one, all right. And then that would actually just bring that statements here, phrase one, phrase one to the screen. And likewise, I can echo phrase two. Alright, so the end results of my echoing these two statements is that they are printed line by line, right? Students who came in will who came late. And then you notice late and in are kind of joined because there’s no space because the strings were kind of, well, I didn’t put a space in either one, I should have either put a space here, or a space here for the echo statements to do that. But you notice once again that they printed out in one line and we already ventilated that it’s best to print the tag that they should be printed in. But today, we just want to look at string manipulation. So I’ll just keep it rather simple for now. Alright, so the first thing I want to bring across is a thing called concatenation, which is actually the joining of two strings. So let’s see, I wanted to join them together. But we just did that right? We just joined them. And you see that they are there. But then there is no space here. And what if we wanted to inject something else in between the text that is already in the tool variables. So concatenation actually allows us to put blocks of string together, whether it’s a string variable, or it is a static string, meaning I am declaring just opening quotation marks and typing strings. If I want to put them together, we call that concatenation. And in PHP, we use a full stop to concatenate. So if I say echo phrase, one dot and then freeze two, then I get the same effect. When I refresh. I just changed the code. And I’m just refreshing the page and you see that it’s the same text, alright, but then I need a space I’m not going to modify these two, I don’t know if these two should or should not have a space, I’m not going to put one there. But I know I want it printed with a space in between. So what I’m going to do is say dot and then I’m going to open quotation marks, and then put in my old static string, which in this case can be a space. And then I use another dots. So each time I use a dot, I’m actually joining a string to another string. So in other words, I’m joining the string that is in phrase one, which is students who came in late and then I’m joining that onto a string that I only put a space in. And then I’m joining all of this further onto another bit of string called the which is found in freeze tool. So when I refresh this, I will see it printed with that space. Alright. So by extension, if I wanted to add more to that sentence, students who came late and then what if If I wanted to specify, and then I said maybe comma. And then I said named sorry, then I said named Tiffany. Comma. All right. So what this is going to do is say, join on phrase one, which is students who came late, and then join on the comma named Tiffany, and then comma, so everything inside of the static string will be joined on to this to the right of this and to the left of this. So when I refresh that sentence, then it should include all of that, right? And then you notice I don’t have a space here so I can just go back and edit that string and say comma, space, Tiffany? All right. I know when I refresh everything looks like Elisa fearly properly formed statement. All right. Now of course, grammatically, it may be incorrect. But at the end of the day, I just want us to appreciate how we can go about doing what we call concatenation. Now moving on another cool feature that it gives me as it exposes certain built in functions that allow us to set if we want letters to be uppercase or lowercase. All right, so I’m going to create a variable and call it name. And I’m going to put my name in there. So my name is for vor Williams, but notice I’m writing it’s all in lowercase. All right? No. If I wanted triple treble or Williams to be printed with uppercase letters, at least the first letter, then I will do something like this. So here I have an echo statement, I’m just going to send an uppercase first letter. So I want the first letter in this variable to have an uppercase letter so that the first letter in this variable is t Alright, so even though it’s Trevor Williams, that’s the first letter in that entire variable. So if I wanted that uppercase, then I would use that function. And we can see here Well, this is not printing out as we desire. And once again, that’s because remember, the echo does not break line automatically. So what I’ll do is just include an echo and put a br tag right here, so that we have that brake line after this concatenation exercise. Alright, so once again, let me just redo that. And there we go. So uppercase first letter, and we’re upper casing t in the variable name. Alright, so just a, another change, I just remember that we should start off with our h1 tag at the top to indicate which beads were on. So apologies for skipping that earlier. But essentially, our very our function for that is UC first name. And notice I’m still using a concatenation. I’m a co I static string here. Yes, I use single quotes. So I’m just showing you that you can use single quotes or double quotes. And we said uppercase first letter. So the demonstration is that we are upper casing. ucaas means uppercase, and if you hover over it and you have the requisite extensions installed, then you will get a bit of explanation as to what that function does. And it takes some string as its parameter. So I’m passing in name, which is a string variable, and then it is upper casing, the first letter in that string, and then I’m further concatenating a br tag. Alright, so the concatenation works with functions as long as you’re expecting a string, you can see static string concatenate on to the result of this function call, which should be a string, which would be the, the transformed upper, the upper case of the first that there are this string, that’s what this function will return, which is also a string, so concatenate, that’s To this sentence, and then I’m further concatenating a br tag. Alright? And then what if I wanted each word to have properties? Right? So I’m just going to duplicate this. And I’m going to say uppercase first letter off each word. Alright, so this echo statement is going to print out, or at least I’m going to change the function that is being used here. So you have you see first then you have you see words. Alright, so you’ll see words, actually uppercase is the word each word phone in the string. So that’s one word, a uppercase is the first letter of that word. This is another word, it’s uppercase is that one. So if I put in phrase one, then I would get a capital S capital, there’ll be a capital C, capital L. If I did this one, it will be capital I capital C, et cetera, etc. So because I’m doing name, I expect to get the capital T and I kept it W and when I review it, The results of that there’s my h1 tag, and I review the results I’m seeing for vor Williams sorry, and I’m seeing exactly what I expect. Now, if I wanted everything to be opportunities, then I could use and I’ll just say echo upper case. Alright, and I’m going to type this one from scratch. So we’re going to do some concatenation, again, some uppercase, and then I’m going to concatenate onto the result of the new function that we’re going to be looking at named str, upper str. str to upper brother. So I want to say str tool and getting a bit of healthier from Visual Studio code. And then the function is str to upper and they’re showing me to the right what parameters I need to pass in which would be a string. So I can passing a variable that has a string as we’ve seen here, right. So if I pass the name, then the expectation is that everything will be uppercase. I’m going to pass in name, I will just test that theory. And I’ll just concatenate a br tag at the end to make sure that we get a brake line afterwards. And when I refresh this, then Trevor Williams is printed in uppercase. All right, so these are some useful little functions that are there. All right, and then in the same way that I have to upper I have to lower so I’m just going to duplicate this line, and I’m going to see lowercase. And then this function would be str to lower. Alright, and since all of my text is already in lowercase, I’m going to show you that you can pass in a variable or you can just write the string that you wish to enter. So I’m just going to replace the variable With an uppercase sentence, so this is all upper or rather this was all upper case. Alright, so the expectation is that once we pass any bit of text into the function str to lower that it will become lowercase text. Alright, so once I refresh, then we see I wrote it in as all uppercase, so I’m sure you can appreciate if I echoed it like this, then it would come out in all caps. And I passed all these capital letters into the function str to lower, which effectively converted everything into lowercase. Alright, so so far some further distinction between our sections, I’m going to put in some HR tags, but then remember, you kind of put in the HR tag by itself. Once you’re inside of a PHP di you actually have to echo this screen. So if I want a tag, I simply echo So I’m just going to echo this HR tag between the concatenation section, and the case transformation section. All right, and then we will be moving on to some other little features that help us to repeat. So the first one that we’ll be looking at is one that helps us to repeat a string. So I’ll say echo, and repeat string. And then I’m going to concatenate onto a function called str, underscore, repeat. Now, you may be seeing, oh, this may be a lot to remember, you know, the kind comments all of this time around. That’s fine. I mean, it’s years of practice that allows me to do this so I can appreciate it is daunting, but just remember, you can always research you can always find resources to help you find the easiest way to accomplish what you want. But this is good knowledge regardless because then it helps you to be a more rounded PHP developer. All right, so I am going to reprint repeat string, and I’m concatenating that static text onto the result of whatever this function is going to return to me, because that’s a value returning function. Essentially, if you haven’t done functions, that’s fine, but this is going to return a value. And that value is what will be printed, it will be concatenated into this statement. Alright, and then I’m going to concatenate a br tag. Alright, so str repeat actually helps us to set one value, whether it’s a variable, whatever it is, that you want to repeat, and how many times you want to repeat it. So let’s say I wanted to print name or my name 10 times, all right. Well, it starts in but I want it to repeat the letter A 10 times so the first parameter in str repeats is a or the or the string that you wish to repeat Alright, so when he gets into Photoshop, he starts talking about parameters and values and the value types that needs to be passed in. So the first one is a string, it has to be a string, because this is a string manipulation function, we pass that in by just you know, making reference to the value. And then we say comma, and the number of times that we wish to have it multiplied. So you see I’m getting some help here from Visual Studio code where it’s seeing, okay, the first variable needed to be string check, and the next one needs to be an integer or a number, which would determine how many times I repeat which means I can’t put another string there or as it will break. So I’ll put 10 because I want the letter A to be printed 10 times and then if I refresh and everything should look a bit differently with the lines here we go. Repeat string, we have a occurring 10 times if I change that string to be repeated. Once again, I could put in a variable so I could put in the variable name And there you have my name being printed 10 times across the page. Now I extend this a bit more What if I wanted, let’s go back to the letter A. So I’m just going to undo and go back to a, what if I wanted a to be printed in all caps, I wrote it as a static common key is what I wanted in all caps. Of course, we could just do with that. But I mean, let’s, let’s face it, that’s a bit too easy. So I know that that needs to be difficult, but I just tried to show you something that you could actually nest all of this inside of another function. So I could say, str to upper. And then I’m passing in all of this. So let me break that down for you. I’m seeing str to opera which we looked at before which takes a string as its parameter. And then as so as its parameter, I’m passing in This other function call, which returns a string, and their string it’s returning is the letter A 10 times so it’s returning the common letter A 10 times, and that’s going to come back as a string. And then what we’re doing is calling str to opera on top of that string that is being returned. So in other words, and I really should have duplicated this, so I shouldn’t have erased it. So I’m just duplicating it so that we can see them side by side, or at least, you know, on the same screen. So str str repeats is printing 10 lowercase is no, I’m putting those 10 lowercase is inside of another function called str to upper which should capitalize everything in that. So when I refresh this screen, then I get the first one which is all lowercase is and then I get the next one, which is all uppercase is. Alright, so the you can actually nest functions, that’s what we call nesting. Combine them to get your desired result. All right, let’s move on to some more fun stuff. So there is another function here called substring. That allows us to get a portion of a string. Alright, so I just typed that so quickly, and this is the, this is the form off this substring function. So it’s really sub str. And what it does is take a string variables anything a string value, so they stored it in a variable, you’re typing in the value manually. And it says, from what starting points to what ending point do you want me to extract So in other words, trouble or Williams between the two words, this has seven letters and this has it all right. What this is sub str function is seeing is that start from the fifth letter. So the fifth letter here is the OHL and get me up to the table. letter. All right. Now let me retract that it says, starting at this character, get me this many characters afterwards. That’s what he’s really saying. Alright, so in other words starts at the fifth character, which in this case would be 12345. So that is your starting points. And it really means start for 10 characters after the fifth character. All right, so five means give me everything after the fifth character. And then this number is as many as 10. And spaces do cone So the one thing that the space is exempted, alright, so it would be 10 spaces from here. Alright, so without counting that I’ll actually just run the code. I will see what happens. So we’ll just go over here, refresh. And there we go. So you started at all and that’s the fifth and then it goes on and it’s getting 10 chiropractors In the string from starting from the fifth one, so if I just decided I said maybe three, I want to start from third card and get five characters afterwards, then that whole dynamic will change. And I’m just getting the end of my name, which is vor. And the space actually, all right, so that’s 1234. And the space makes five characters. Okay, so that space, bear in mind that this space is important. Alright, so I just added a bunch more functions here. And we can just go through them because I think we have the general idea. Each function does something differently. And you know, it allows you to be creative and to explore the code, so I’ll just push through with all of them. So we have another one here that says get possession of string, meaning you use str POS, that’s the name of the function, and you will pass in the string and you see what character or character sequence you Like to find the position of in the string. No notes strings are just like arrays or arrays are just like strings, meaning what strings are just like our ease, meaning the first letter is always zero. All right, so that’s why even with our substring, you would have started after the third card. So because it’s really 0123, so it’s really the third cards by the colon team like it’s an array, why it would have started on the fourth one, alright, so just the same way, when we get position, remember that the first very first character in any string is always going to be zero. So if I said find a position of the W, and I refresh on the when I do that, you’re going to see a bunch of things, but the position of the W is eight because I have 01234567, the space codes. Alright, the space those columns are seven, and this would be the eighth because it’s started counting from zero. All right, so that is how that function works. And then for the other ones, we have one that says fine character, and we just pass in the character. So we have str car and we pass in the string and then the character that we want inside of that string, and it will return you know where it is kind of it will kind of show you where it’s starting from, or at least the first occurrence of that character. So it is case sensitive in this case, because fine character, capital R didn’t work because there are no capital RS inside of that string called name. Alright. Alright, so adding some more functions to this, we will now look at how we find the length of a string. So the length of a string would be found by str lane which which will just return how long the string is if you want to know how many cars Whoa, hold on I string it’s how long a word is so long as sentences, how many characters are in that contiguous block of text. So when I refresh this, we first look at str Lin. And the length of this string of My name is 16. So we’re finding the length of the variable name. And that means that if it starts counting, sorry, it starts counting 1-234-567-8910 1112 1314 1516 characters. Now please note that the length is how many there are, but if we’re going to call it as whole, the other functions would have been processing the string. Well, we start counting from 0123. So it’s always going to be n minus one, where n is str LNG. You can always run a variable if it is a meter or like a for loop, over a string or something like that. Always Remember that it’s the lane that whatever valid Lin, the str Len returns minus one. All right. Now back to the other function. The next one that we want to look at is the trim function. So firstly, I echo that sentence here without a trim, we have the letter E, then we have another, we’re concatenating, that letter A onto a string with a space B, C, D, and space again, then eat. Alright, and if we look at it, then we see that we have it without any trim. So without trim, we have a space B space either and that’s human readable, that’s fine. All right, then the next operation that we carry out is to actually trim the spaces on both sides. So this has no spaces in it, that’s just the letter A but then I’m going to run the trim function over this text. So what trim does is take off any spaces that might be to the left or to the right of the string. So remember, all of this is a string, it’s not looking at each letter, it’s just looking at where the quotation marks start where they end. And then it’s going to say, is there any space between the first character and the open quotation mark, and the last character and the open quotation mark. And then if we put it inside of this trim function, it will see remove all the spaces that are there. So if we look at the output of this line here, four to one, then we will see that it will have a and b joined because there is no longer any space between A and B, and D and E joined because there’s no longer any space but C remains the same, because once again, the trim only works on the extreme left or the extreme right. All right, then variations the true are l trim and our trim the L means left on the IR means right that’s all there is to it. So the L trim just says trim all spaces to the left and Archer means spin Trim all spaces to the right. So trim those on either side or both sides. And then you have functions for either side. And if we look at the output of those, then we see that for the L trim, the B has lost its space, but the D maintains its own. And then for there are trim the A, the B maintains its space, but the D has lost its own. So that’s really the difference between those two. But once again, trim those either side regardless. And then the final one that we look at is the Replace. So you can use str replace, and then you pass in your string that you’re looking for the string that you want to replace it with. And then you pass in that string or the variable with a string that it should find this in and replace with that. Alright, so when I refresh, I know that none of these are actually making changes to the actual variables. They’re just meek allowing you to make changes on demand while maintaining the actual string. All right, so if I look at the output for that, then we see that we get in class sit with rock, where initially it was stand with rock. So we have the original thing being printed out here nonetheless. And then we have the modified one here, because on demand we just wanted to see see to throw up instead of stand with the rock. Well, that’s it for now with our string manipulations. There’s a lot more to it, but this is enough to get started and enough to get you excited. And if you have any questions, comments or feedback, feel free to drop me a line and let me know