TOP 100 PHP Functions ( 11 - 20 ) | PHP Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's going on it's clever tikki and in this video we're going to continue covering top 100 PHP functions and in this video we're gonna cover function 11 through 20 alright so the first function on the list is preg match and this function I use this one a lot to parse out the content from websites and it performed performs regular expression match so let's break down the function so first of all it returns an int so it's going to be true if there is a match and false if there's no match so if we just wanted to check if the value or string is found on a webpage or something like that we would just use an if statement and we we could continue using the program or running the script if the if the string or the pattern is found on a web page otherwise returns zero if there is no match though this function is a lot more complex so we can use match to store our match inside of an array which is really awesome so that we can continue working with whatever match that the function has found and first we provide pattern which is a regular expression pattern then we provide actual subject like a website content or a text file and then the actual match where the values or the match is going to be stored and finally there's optional flags as well alright so let's let me give you an example here so if we have some HTML suppose we have an image that we're trying to match and I'm just going to use my website as an example here just gonna provide a URL to the image one of the thumbnails on my website and suppose we wanted just to extract this URL because we want an either to download the image or displayed on our on site so here's what a pragmatic an come in and first we use the regular expression so it's a pattern surrounded by the exclamation marks and here we can just say IMG SRC equals forward slash double quote for slash double quote and inside the double double quotes we can just say dot star question mark which means match anything inside of the double quotes and the reason I put this in the brackets is because it's going to be captured in our match array and put inside of the next array key so let me show you what I mean so here we provide the actual subject from which we're finding this regular expression and then we're storing everything inside of the match array so if I print the match right here and then view it okay so this whole regular expression pattern is has been stored inside of the zero array key and the parenthesized content is stored in the next array key right here so hopefully you can see how that works so let's suppose I also wanted to capture the extension here so I can say dot and then PNG inside of the brackets so now we can also capture the extension of this image and it's going to be in the next array key of the match just like that so the more parentheses you have the more keys PHP is going to create storing all those matches inside of different arrow keys okay so I just wanted to make sure this part is that you guys understand this part because I was really confused when I was first working with this function all right let's see what the next function on the list is is sprint F okay let's go ahead and look it up on php.net manual so this function honestly I haven't used too much but as I started learning about it I realized just how useful it can be so it returns a formatted string and the return value is going to be a string so so in order to understand this first look through these characters here so those are basically all kinds of different ways to format the actual variable that you're going to be inserting inside the string so just go over these and look up what all of these values mean in this video I'm just gonna be using the percentage symbol which is what we use to start inserting variable values I'm also gonna use D which is the basically the digit and s which is a string and F which is a decimal string or float and it gives you this list and it's just a really flexible way to create all kinds of different formatting types inside of your string if that's what you're looking for so this is the actual outline of how the function works but let me give you an my own example here so so let's create the string that we're gonna be using for our formatting first so let me just type this up here so it's gonna have a digit it's going to have a string inserted it's gonna have another string inserted it's going to have a float and another string okay so what the hell is this okay so now basically you can think of the format part as a template and this template has placeholders for the future variables to be inserted inside of this string and not only do we specify placeholders we also specify what type of value that placeholder should be converted to okay so these placeholders obviously need to be replaced by the actual variables so let's create those variables gonna create these variables now you're gonna be numbers and strings and you guys will see what all this means when we run the actual function if this doesn't make any sense right now just stay with me here it'll make sense okay here's where we actually use the functions so the first value is format just like specified in the manual so that's our template that we created here so let's specify format and then okay so here's the part where PHP is going to know in which order to place those variables so the first variable is going to be the first variable that we provide here is going to be the first one that gets replaced here and the second one is going to be the second one the third one is going to be the third one etc etc etc so if we want to put our number variable here this is the variable that we should provide here and then this number will be inserted instead of this percentage D symbol and it's going to be 4-minute as as d which is a which is a argument that is treated as integer and presented as a signed decimal number so basically an integer okay so the next one I want to use is animals and that's gonna be an percentage s which is a string and that's the type of our variable so that's gonna be the second one the third one is location and then number two and this will number I don't know I'm just gonna be converting into decimal just to show you guys an example and then finally insects is the last variable so just make sure you understand that the order that we place these variables here is gonna be the same order that they're gonna be placed in our template or format here because that wasn't so clear to me when I was first using this function all right so now we're gonna PHP is going to echo out the final formatted string which is gonna have our original string replaced with all these placeholders that we created and formatting them according to the format specified alright so let me see what it looks like there are 10 seagulls by the beach and forty four point zero zero zero zero zero zero butterflies okay so hopefully right there it all came together for you and you can see that the placeholders have been replaced by the actual variables and they have been formatted with whatever formatting that we specified and feel free to use all these other formatting options play around with them see what they do and hopefully this function will be useful to you someday all right so next up is pragmatics printf is trim okay so this function is pretty useful when you want to make sure that there's no extra characters surrounding the text so I usually use it when parsing out content from other websites and just making sure that there's no extra spaces or anything like that remaining inside of the string before placing them inside of the database for example and let me see let's look it up and PHP many all so strip whitespace or other characters from the beginning and end of a string so that's exactly what it does and here's a list of characters that are it replaces from the beginning and end of the string and we can also specify our own character mask which is our own characters that we want to strip from the string so let's just say we have some text clever techy and then let's suppose I just wanted to remove C and E from the end of the string so I could just use trim text and then provide those exact characters and here what's important to understand is that these are not the characters that are going to be removed from the whole string they're gonna be removed only from the beginning and end of the string if they exist so here C exists in the beginning so that's the one that's gonna be removed or trimmed and exists at the end of the string so that's gonna be removed from the end of the string as well so now we get leverage take okay let me just print out break in tag so you guys can see this okay so we get elaborate techy without the e at the end and you can see that the C has been removed as well so that's how that function works and in its default state you can just use trim text and it's going to remove any spaces around in the string wait what happened okay so that you can see that it's not removing the C and E because it's no longer the case because we now have spaces in the text and in the next text that's printed there's no extra spaces surrounding that text okay so that's how that works next is STR to lower very self-explanatory and I'm not even gonna look it up in PHP manual because all it does is converts all text to lowercase so if I have some text here and then I can just use STR to lower it's going to lower the text to to lowercase and that's all it does let's create something awesome with PHP in lower case alright next function on a list this file exists and it does exactly what it says it does it just checks if the file exists so let me just go to some random directory here and check if this nature one that jpg exists just copy the whole file path and I know that the file is in fact there so it should exist and if the PHP says it doesn't exist then it's lying so I can use an if statement say file exists if file exists file then we can echo out the file exists else echo file doesn't exist all right let's see if PHP is going to lie nope it's telling the truth the file favorite one words nature nature one that jpg exists however if I change the extension to give it's gonna say it doesn't exist and that's how easy it is to check if the file exists with file exists function next function is is string so also pretty self-explanatory so if we have text which is a string we can check if it's in fact a string and then say the text is a string as simple as that the text is a string also it can be kind of confusing just what passes as a string so I'm taking this from php.net manual so if you have an array of values like this I'm just gonna type all of them here and then I'll explain what's going on I said hey what's going what's going alright so let's loop through this array now and find out which values are string and which ones are not and I'm just gonna tell you guys right now that whatever is inside that the single quotes is a string so that is a string that is a string that is a string this is a string this is a string and this is a string and the rest are not strings and I guess these are kind of tricky here because those are numbers but they're still considered a string string type string datatype because they're inside the single quotes whatever is inside single quotes is still going to be considered data type string and obviously here it's going to be an integer he is going to be decimal this one's gonna be an integer null true and false are not a string either so all right so let's just create the loop to loop through all of these values and I'm just gonna copy this code from PHP manual so what they did here is they used two more functions so one of them is VAR export which exports the actual value of the array without printing it to the certain screen or anything like that so that's one function you can use and then they also use var dump and they used is string inside of var dump to check if the value is a string so what do we get in the end let's check it out let's go to view source and as you can see it's looping through the whole array and then it's using extract its using var export to get the actual value here and print it out so we know what kind of value it is and then var dumps it and says ah checks if this if the value is a string and then outputs false or true if it is in fact the string so it can be kind of confusing here but if you look through the function and understand it it's pretty easy to understand actually so hopefully I didn't confuse you guys too much with this with this loop you can just look through the array yourself and understand that which values are strings and which ones are not okay so that's a that's a history let's move on here to Prag replace all right so this function is pretty similar to pragmatics an also use a replacement so it's called pellagra plays because we're also able to use a pattern of regular expressions and then we specify the replacement to replace it with and of course the subject which is the actual string or whatever it doesn't actually have to be a string can be of any mixed value type so search is subject for matches to pattern and replaces them with replacement pretty easy you understand all right so let's suppose we have some HTML once again I use these functions like including pragmatics pragmatics all in preg replace to parse HTML a lot so that's why I keep using HTML as an example so let's suppose we have some random text that is inside the strong HTML tags right and let's suppose we wanted to replace these tags with italics so that's when we can use practice and we can say for every place and then mixed pattern which is our regular expression pattern in the same way that we did that we use the regular expression here and Prag match and here in order to match this text so can just say don't forget to use excellent exclamation marks you see you strong clause a strong tag and then use that parenthesis with dot star and question mark so that's going to capture whatever text is in between the strong tags and now we can replace it with italics and here's where it gets a little tricky in order to get the get the actual content that is between the strong tags so here we captured it and here we can reuse that capture using dollar sign one so one is going to be the first capture match two is going to be the second capture match etc etc etc so in a similar way that we captured those values with preg match we're doing it in a similar way here but we're replacing the value with that content instead of capturing them inside of the array and then we provide the actual HTML or subject text so now we should have our regular HTML which is going to be bold and then we should have italic HTML as well so let's see if that works bold HTML italic HTML a font you if I go to view page source you can see the strong text here and then italics on the next part of the text alright so that's how awesome and powerful Prag replaces again guys if you want to understand regular expressions better just watch my video on PHP regular expressions where I cover these in depth and I have a really awesome cheat sheet for you to download as well alright so the next one is file get contents and this one is pretty useful when it's a pretty useful alternative to curls so if you don't want to type a bunch of curl you can just use this function and you can say for example file get contents and then you can just get the contents of any webpage that you want it's not as flexible as curl so you can provide any kind of options to extract content so this function can be used but I guess it's a faster alternative to curl and you can only use it if you just want to quick quickly scrape some content from a web page so that's how you would do it and that will get you all the content from Google refresh the page here actually close this refresh and that should get it get us the Google home page and there it is so that we can extract whatever we need from this page using our pragmatic or Prairie police functions I'm just gonna come into South so that our next functions are are loading faster all right so next one is air a key exists and it does exactly what it says it does it checks if the air a key exists so if we have an array say we have an associative array with some car details with ear make and model so we can just say suppose we wanted to make sure that the make key exists in this array so we can just say if air a key exists make and so we first provide the key and then we provide the array from which we're checking for that key so then we can say echo the key exists else echo the key doesn't exist so it should print out the key exists if PHP is not lying the key exists awesome okay so you can also insert ear or model and I will say it exists as well or we can also create the loop to loop through all the arrow keys like this and then instead of on the make here we can just put the key and say the key exists he doesn't exist so here obviously all the keys are gonna exist because we're checking for those keys and it's just gonna say the ear exists the make exists and the model exists so that's how we can check for the existence of array keys and that's pretty much all that the function does and the last function on the list is array keys to extract all the keys from the array which can be super useful when you're working with multi-dimensional arrays or extracting data from the array and you need to access the keys very quickly so here I'm just gonna print our use array keys and then array that we created earlier and that's just gonna print out all the keys that exist in the array so it's going to be year make and model and as you can see it put it inside of the numeric array just like that so that's the last function on PHP list and that's it for the top 100 PHP functions covering function 11 to 20 so that's pragmatic from here to array keys which is number 20 and in the next video we're going to cover function 21 to function 30 so all of these functions here from their name to is null and I hope you guys liked this video if you did please like share and subscribe it really helps and I'll see you in the next video clever take it out
Info
Channel: Clever Techie
Views: 7,851
Rating: undefined out of 5
Keywords: php function, php functions, php tutorial, clever techie, top php functions
Id: rQxnnWg4XqQ
Channel Id: undefined
Length: 27min 7sec (1627 seconds)
Published: Sat Oct 27 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.