How can detect text file line breaks in php?

Go To StackoverFlow.com

0

I have a txt file that created in notepad , containing these lines for example :

lao_esan_ubon_444
chantrick
solikh
knalpoot
tanduy_007
Mario3010

Now i need to put every line in an array cell , like these:

array('lao_esan_ubon_444','chantrick','solikh','knalpoot','tanduy_007','Mario3010');

How can i do this with PHP (for long lists)?

2012-04-03 19:47
by AMIN Gholibeigian
Mark: The file function combines both of these - jnylen 2012-04-03 19:51
Didn't realize that about the file function. Splendid - Mark Biek 2012-04-03 19:51


3

Use the file function.

$array = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

Note that the flags parameter (FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) is only available in PHP 5 or greater.

2012-04-03 19:50
by jnylen
Your code is awesom - AMIN Gholibeigian 2012-04-04 08:49


2

<?php
$data=file_get_contents("your file name");
$array=explode("\n",$data);
?>

hope this helps

2012-04-03 19:57
by Nishchal Gautam