Home >Backend Development >PHP Problem >Convert php string to array based on newline

Convert php string to array based on newline

PHPz
PHPzOriginal
2023-05-05 22:14:091212browse

PHP is a widely used programming language, especially in the field of web development. In PHP, we often need to process strings. One common need is to convert a long string containing newlines into an array format.

In this article, we will introduce how to use PHP to convert a string containing newlines into an array, and show some useful tips and methods in the process.

Newline characters in strings

First, let’s review how newline characters appear in strings. In Unix/Linux systems, the newline character at the end of the line is a \n character; in Windows systems, the newline character at the end of the line is a \r\n character. When we edit or read text files, we encounter these different end-of-line newlines. For example, the following string contains multiple instances of different end-of-line newline characters:

Hello\nworld\r\ndoing things

To convert this string into an array, we need to consider how to correctly segment the string and how to process it different end-of-line newlines.

Use the explode function

The explode() function in PHP can split a string into an array by specifying the delimiter. For a string containing different newline characters, we can use regular expressions to describe the delimiter.

For example, the following code can convert a string containing \r\n and \n newline characters into an array:

$str = "Hello\nworld\r\ndoing things";
$array = preg_split('/\r?\n/', $str);
print_r($array);

The output is as follows:

Array
(
    [0] => Hello
    [1] => world
    [2] => doing things
)

In this example, we use the regular expression /\r?\n/, which can match \r\n and \nTwo end-of-line newline characters. Therefore, we can split the entire string into an array of three elements.

It is worth noting that since the regular expression itself also has some special characters, they need to be escaped before use. In PHP, we can use the preg_quote() function to quickly escape special characters in regular expressions.

$delimiter = "/".preg_quote("\r\n")."|".preg_quote("\n")."/";
$array = preg_split($delimiter, $str);

Handling spaces at the end of the line

In addition to the newline character at the end of the line, we also need to consider that there may be spaces or other invisible characters at the end of the line. These spaces will not affect the display of the string, but may affect the conversion of the string into an array. For example, the following string contains newlines and some spaces:

Hello   \nworld \r\n doing things     

Split this string, we will get an array containing empty strings and spaces:

$array = preg_split('/\r?\n/', $str);
print_r($array);

Output result:

Array
(
    [0] => Hello   
    [1] => world 
    [2] =>  doing things     
)

This is not the result we want. To avoid this problem, we need to remove the spaces at the end of the line before splitting the string. You can use the trim() function to remove whitespace characters at both ends of a string. For example, the following code can remove the spaces at the end of the line and convert the string into an array:

$array = preg_split('/\r?\n/', $str);
foreach ($array as $key => $line) {
    $array[$key] = trim($line);
}
print_r($array);

Output result:

Array
(
    [0] => Hello
    [1] => world
    [2] => doing things
)

Conclusion

Convert the string containing the newline character Arrays are a common processing requirement in PHP. In this article, we introduce how to use the preg_split() function and regular expressions to split a string, and show you how to deal with trailing spaces. I hope these tips are helpful in your PHP programming endeavors.

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

Statement:
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
Previous article:php array reset key codeNext article:php array reset key code