Convert text in array of lines with constant PHP_EOL
9 giugno 2008, 8:15 Open Source , PHP , Tips & Tricks June 9, 2008, 8:15
When you write web applications in PHP happens very often having to handle the data in a text file.
The best method to acquire data of this type is undoubtedly the ` file `, which just returns an array containing all lines of text files.
But as often happens to have anything to do with PHP script designed to run on older or abnormal situations, such as embedded servers that provide a limited range of functions PHP, or even scripts that process the lines of text at the same time where they produce it.
In these cases you can use a syntax less immediate but equally effective, resorting to `explode is the system constant PHP_EOL.
Here is a simple example:
<? Php = <<< EOD $ txtMessage ROW # 1 LINE # 2 LINE # 3 RIGA # 4 EOD; $ ALines = explode (PHP_EOL, $ txtMessage); echo "<table border=\"1\"> \ n". PHP_EOL; foreach ($ aLines as $ strLine) { echo "<tr> \ n". PHP_EOL. "<td> \ n". $ strLine. "</ td> \ n". PHP_EOL. "</ tr> \ n". PHP_EOL; } echo "\ n". PHP_EOL; ?>
The result is as follows:
| ROW # 1 |
| LINE # 2 |
| LINE # 3 |
| RIGA # 4 |
The constant PHP_EOL equivalent to end of line character of the operating system on which we are working on the script: use this constant not only improves the readability of our program, but also its portability.














