Webdesign,code & ...stuff!

Aug 12, 2009

PHP_EOL : PHP End Of Line

PHP_EOL is apparently used to find the newline character in a cross-platform-compatible way, so it handles DOS/Mac/Unix issues.

Most of the PHP I write is for generating HTML, and I use <br /> instead of actual newlines, so haven't used (I don't even know) this constant before. Just until I need to
explode a block of string to be separated by the end of the line. It's something like the file() function can do but I only want to parse a string, I dont want to put it into another file.

There were no \r\n added to the end of each line on the string that would be used as identifier for the explode. I know I can just add it myself but the string is dynamic, it's from a user input.

So I googled for some workaround but no luck, I don't know, may be its my search keywords. I wander at php.net and landed on
this page.
There I found about PHP_EOL, so I tried to use it and voila, works like magic.

Sample code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
$str
='IT sa CITE Batch 6
Glendon Alimin
John Reeve Belarmino
Carl Anthony Campos
Alex Culango
Joenar Dalumpines
Ryan Dupay
Victoriano Eco II
Ronald Enclonar
Johnric Godinez
Jerry Laborte
Jim Carl Luzano
Licerio Maeza
Joseph Marvin Magdadaro
Ramil James Mangapis
Ryan Christoper Maramot
Mitchell Masinas
Paulino Muñez
Francis Ray Ranile
Reynold Salceda
Jeb Agustin Saldariega
Rholyn Sampan
Kenneth Sotto
Michael Sumayang
Chrissan Versoza
Roel Villarba '
;

$arr=explode(PHP_EOL,$str);
print_r($arr);
?>


the above code returns:
Array
(
[0] => IT sa CITE Batch 6
[1] => Glendon Alimin
[2] => John Reeve Belarmino
[3] => Carl Anthony Campos
[4] => Alex Culango
[5] => Joenar Dalumpines
[6] => Ryan Dupay
[7] => Victoriano Eco II
[8] => Ronald Enclonar
[9] => Johnric Godinez
[10] => Jerry Laborte
[11] => Jim Carl Luzano
[12] => Licerio Maeza
[13] => Joseph Marvin Magdadaro
[14] => Ramil James Mangapis
[15] => Ryan Christoper Maramot
[16] => Mitchell Masinas
[17] => Paulino Muñez
[18] => Francis Ray Ranile
[19] => Reynold Salceda
[20] => Jeb Agustin Saldariega
[21] => Rholyn Sampan
[22] => Kenneth Sotto
[23] => Michael Sumayang
[24] => Chrissan Versoza
[25] => Roel Villarba
)

3 comments:

mariancalago said...

ay,,, wala akong alam sa php.. pero magtry ako nyan...

marian-moviereviews said...

sa hmtl may alam ako kunti lng.heheh

rencalago said...

thnx.. i already added both your links in my page.
thanks