PHP Front To Back [Part 12] - Common String Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay what's going on guys in this video we're going to be talking about some of the common string functions in PHP okay so functions that we use to deal with strings now I don't know if you already know this or not but if you go to php.net and then slash whatever function you want to look up yeah you can do that and for instance we're looking at print R which just prints out a raise we already reviewed this in the past and it gives you just the description of different parameters that can accept the return values some examples and some user user contributed notes so just to let you know that you can do that all right so I have a new file called string on the score functions PHP and we're just going to take a look at some of the different functions that PHP offers so the first we're going to look at is substring or our sub STR and what this does is it returns a portion sir above that returns a portion of a string so let me give you an example if we were to create a variable called output and we'll set it to sub STR and then it's going to take in a parameter which will be our string we'll just say hello and then we want to say where we want the substring to start so let's say one okay and then we'll just echo out output now if we do that we just get e ll oh the reason for that is we said we wanted to start it the one one index so it goes 0 1 2 3 4 so it's going to start at e because that's the one position all right if I were to change this to 0 then it's going to start at the beginning if I change it to let's say 3 reload we get just ello all right now we can also define what we want it to end so let's say we want to start at 1 which is e and then we want it to end at the 3 position and we reload and we get e ll because 1 is e and then there are 1 2 3 ends right here ok after this second out all right and then if you want to start the back you can do that as well by using a negative number so if we say let's see negative two we'll just comment it so reload we get ello okay because that's the negative two position and it goes to the right alright so that is substring pretty simple next one we're going to look at is let's see string length or STR le N and that will return the length of a string okay so this is pretty simple let's go ahead and create a variable called output and we'll set it equal to STR le N and let's say hello world and then we'll echo out output haloed and we get 11 okay so it's just counting the characters including the space all right so it's a pretty simple function okay so next we're going to look at its string position or STR POS and that will find the position of the first occurrence of a specified substring so let's create an output variable and set it to XT our POS and we're going to put in the string which is we'll say hello world and then let's search for the oh okay now what this is going to do is start at the beginning here and once it find this match it'll give us that position number so let's say echo output and that gives us 4 because it's 0 1 2 3 4 ok so pretty simple now there's also a function called STR rpoS so let me just copy that and comment so so STR our POS and what this does is it finds the last occurrence ok so we'll change this at an R there and then let's save that and reload and we get 7 because it's looking at the last occurrence which is this so zero one two three four five six seven okay so next one we're going to look at is trim and what this does is it trims white space so as an example let's create a variable called text and I'm going to set that to a low world and then I'm just going to put a bunch of spaces after it like that and then what we'll do is we'll do var dump which will give us some information on this value okay so we'll reload we can see it's a string gives us the actual value and it gives us the number the length of it which in this case is 25 now the reason is 25 is because it counts all of these spaces as one so what we can do is we can set a new variable called trimmed and then set it to the trim function so trim function and I will put in here text and then we'll do our dump for our trimmed and I'll comment this out and save it and now you'll see that this now changes to 11 because it trimmed all that whitespace off okay so this is usually want to use this for like form input when someone submits a form in case they did something like this all right so next one we're going to look at and we just comment too so okay so look next function we're going to look at is string to upper or STR to upper and this will make everything uppercase so this is pretty simple what we'll do is create an output variable and let's set it to STR - upper handle caps in here the little world and then let's echo the output and that gives us hello world in all caps okay we also have string to lower as you may have figured out just comment this out so this is string to lower and this makes everything lowercase okay I'll put that and we get hello world and it's all lowercase okay now if you want to just do the first letter of each word then you want to use you see words okay that will capitalize every word so if we'd say output is equal to you see words and we'll stay in here well actually let's do it all lowercase and then we'll echo that out and I see that the H and the W are both capitalized all right now we're going to take a look at a function called string replaced or STR underscore replace and this will replace all occurrences of a search string within a replacement so if we put in a variable called text and set that to hello world and let's create an output variable and set it to STR or place and this takes in three parameters the first is what we're going to search for which is going to be the word world second is what we want to replace it with and let's say every one third is going to be the actual string or searching which is text okay so let's go ahead and echo output and now we reload and we get hello everyone okay started is hello world but we want to replace world with the word everyone so the next one we want to look at is is string and that will test to see if something is a string or not all right so let me just grab that paste that in okay so let's um let's create a Val or value variable and we'll set it to hello and then we're going to go say output equals is underscore string and then pass in that value and then let's echo output so what that returns to us is a one because it is a string now if we were to change this to let's say a 22 with no quotes we get nothing it actually returns false if we were to put quotes around this and save now we get one because now it's a string alright so pretty simple and then what I'm going to do here is create an array will say values equals array I'm going to put a bunch of different data like totally true false no let's put a string of ABC will put the number 33 without quotes we'll put it with quotes we'll put a float so let's say twenty two point four and then we'll also put that in quotes twenty two point four and see we'll do just nothing we'll do a space will do zero and we'll do zero within quotes alright so what I want to do is loop through these we've done this before for each loop and we'll say for each values as value and then we're going to test to see if that each value is a string we still want to say if is string value and then if it is you want to echo out and let's do value is a string and then we'll put in a line break so let's save that and reload and we get slide zero one oh we got to comment this stuff out okay so we get ABC as a string so you can see it skipped these three because there are the not strings and then 33 it looked at this one because it's a string this one these two are both considered strings and then this arrow with the quotes is a string alright so hopefully that just gives you a better better idea of how that works alright now the last one I want to look at is GZ compressed and that'll actually allow us to compress a string let me just comment this out okay so compress a string so what I'm going to do here is just paste in a very long string okay and I put it inside of a variable called string and then what we'll do is we'll stay compressed we'll set a new variable and we're going to use GZ compressed and pass in that string okay and then we'll echo out compress so you can see what that looks like okay so it's unreadable it's just it's just our compressed text now you're not going to output this anywhere you just want to compress it maybe you want to I don't know pass it through another function or to an API or something I'm not sure exactly but if you want to uncompress it then let's do will set a variable called original and we'll set it to gz uncompressed and we'll pass in the compressed strength and then let's echo that out okay just going to comment that out and then we get the text back okay so you compress it do something with it and then when you need to use it again you can uncompress it all right so that's going to do it for string functions in the next video we're going to take a look at some other functions that have to do with the Rays turning strings into arrays and arrays into strings and so on all right so I will see that
Info
Channel: Traversy Media
Views: 60,897
Rating: undefined out of 5
Keywords: php, php strings, php functions, substr, strpos, string functions, php programming, php tutorial
Id: z32BOHVWplU
Channel Id: undefined
Length: 13min 34sec (814 seconds)
Published: Wed Mar 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.