How does PHP get the contents of a txt file and convert it into an array? (Graphic + video tutorial)

藏色散人
Release: 2019-10-28 18:00:07
Original
7283 people have browsed it

During the development of PHP projects, sometimes it may be necessary to convert file contents into arrays to facilitate data management and analysis. This article will introduce to you how to use PHP to convert the contents of a txt file into an arrayThat is, the specific method of PHP reading the file content and assigning it to the array.

Below we will explain the relevant knowledge in detail through simple code examples:

For example, there is a 1.txt file with the content:

PHP中文网 HTML中文网
Copy after login

So how do we use PHP to get 1 What about the content in a .txt file and converting the content into an array?

The code for PHP to obtain the contents of a txt file and convert it into an array is as follows:

<?php
$data=file_get_contents(&#39;1.txt&#39;);
var_dump(explode(" ",$data));
Copy after login

The main idea of ​​converting the file contents into an array is to first use file_get_contents() Function reads the contents of the file into a string, and then uses the explode
function in PHP to convert the string into an array.

The result of accessing the above code through the browser is as shown in the figure below:

How does PHP get the contents of a txt file and convert it into an array? (Graphic + video tutorial)

As can be seen from the figure, we have successfully added 1. txt file is converted into array form through PHP.

The above is an introduction to the specific method of converting the contents of a txt file into an array in PHP. Hope it helps those in need!

The above is the detailed content of How does PHP get the contents of a txt file and convert it into an array? (Graphic + video tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!