I didn’t notice before that I could use the file function. Today I was making a PHP project to import txt text data into the database. At first, I used file_get_contents() to save the data to variables and then use explode to process it, but later on I found about the usage of file function on the Internet, and I really think that the file function is really beautiful and easy to use. Let’s take a look
Okay, let’s look at an example. Let’s look at the file_get_contents() function first. Now I want to save the contents of the text file a.txt into a data. Below is the content of a.txt as follows.
aaaaaa
bbbbbb
bbbbccc
Okay, just these three lines, now let’s look at the operation method of file_get_contents.
$content = file_get_content('a,txt');
$temp =str_replace(chr(13),'|',$content);
$arr =explode('|',$temp);
It’s not much more than just three lines, so let’s take a look at the file function.
$content = file('a.txt');
It’s done, the effect is exactly the same as above.
Haha, in fact, there are many functions in PHP that we often don’t use. We don’t need to remember them, as long as we know that they exist,
Okay, one final statement: Please indicate when reprinting original articles on this site: www.111cn.cn/phper/php.html