Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert txt into an array in php

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to convert txt into an array in php, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this php article on how to convert txt into an array. Let's take a look at it.

Php txt into an array method: 1, the use of "file_get_contents (txt file object)" statement to read the contents of the file into a string; 2, the use of explode () function to read the string into an array, the syntax is "explode ("\ r\ n ", string object)".

Operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer

How to convert txt into an array in php

File_get_contents () reads the entire file into a string.

This function is the preferred method for reading the contents of a file into a string. If the server operating system supports it, memory mapping technology will also be used to enhance performance.

Grammar

File_get_contents (path,include_path,context,start,max_length)

The explode () function splits one string into another and returns an array of strings.

Grammar

Explode (separator,string,limit)

Examples are as follows:

Example of text content:

I love you, China. I love you, NO.1TOP1123456789123456.

Code:

/ * read the contents of txt files and convert them into an array * / $file = 'data.txt';if (file_exists ($file)) {$content = file_get_contents ($file); / / the file contents read into the string if (empty ($content)) {echo "file contents are empty" } else {$content = mb_convert_encoding ($content, 'UTF-8',' ASCII,UTF-8,GB2312,GBK,BIG5'); / / convert character encoding to utf-8$ array = explode ("\ r\ n", $content); / / convert to array $array = array_filter ($array); / / empty $array = array_unique ($array) / / deduplicated print_r (json_encode ($array));}} else {echo "File does not exist";}

Execution result:

This is the end of the article on "how to convert txt into an array in php". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to convert txt into arrays in php". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development