Home > Backend Development > PHP Problem > php file to string array

php file to string array

WBOY
Release: 2023-05-23 11:03:36
Original
485 people have browsed it

In PHP programming, we often need to read the contents of a file into a variable. In this case, we need to convert the file contents into a string array. This article will show you how to read a file and convert it into a string array using PHP.

1. Read file contents

To read a file into a PHP variable, you can use the file_get_contents() function. This function returns a string containing the entire file contents. Here is an example:

$content = file_get_contents('file.txt');
Copy after login

The above code will read the contents of the file.txt file into the variable $content.

2. Convert the string into a string array

If the content in the file contains multiple lines of text, we need to convert it into a string array so that we can process it line by line. You can use the explode() function in PHP to decompose a string into an array. The specific usage is as follows:

$array = explode("
", $content);
Copy after login

In the above code, use
as the delimiter to decompose the string $content into the array $array. This converts the file contents into a string array.

In some operating systems,
may be used at the end of the line or as a newline character. In this case, appropriate modifications need to be made, for example:

$array = explode("
", $content);   // Windows 操作系统
$array = explode("", $content);   // MacOS 或 Linux 操作系统
Copy after login

3. Remove blank lines and Space

After the above steps, we have successfully converted the file content into a string array. However, in practical applications, we often need to remove empty lines and spaces in the array. This can be achieved by looping the array and using the trim() function. The specific code is as follows:

foreach ($array as &$value) {
    $value = trim($value);
    if(empty($value)) {
        unset($value);
    }
}
Copy after login

In the above code, use foreach Loop through the string array $array and remove spaces from each element using the trim() function. If the element is empty, it is removed from the array using the unset() function.

4. Complete code

The following is the complete code to convert the file content into a string array:

$content = file_get_contents('file.txt');
$array = explode("
", $content);

foreach ($array as &$value) {
    $value = trim($value);
    if(empty($value)) {
        unset($value);
    }
}

print_r($array);
Copy after login

The above code will read the content of the file.txt file into string array $array, and remove spaces and empty lines, and finally output the array.

Conclusion

This article introduces how to use PHP to convert file content into a string array, including reading the file, converting the string into a string array, and removing empty lines and spaces in the array. Wait for steps. I hope readers can practice based on the content of this article and further master PHP programming skills.

The above is the detailed content of php file to string array. For more information, please follow other related articles on the PHP Chinese website!

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