Read each line of txt file to new array element
<?php
$file = fopen("members.txt", "r");
while (!feof($file)) {
$line_of_text = fgets($file);
$members = explode('n', $line_of_text);
fclose($file);
?>
$lines = file($filename, FILE_IGNORE_NEW_LINES);
// Open the file
$fp = @fopen($filename, 'r');
// Add each line to an array
if ($fp) {
$array = explode("n", fread($fp, filesize($filename)));
}
$array = explode("n", file_get_contents('file.txt'));
$yourArray = file("pathToFile.txt", FILE_IGNORE_NEW_LINES);
<?php
$file = fopen("members.txt", "r");
$members = array();
while (!feof($file)) {
$members[] = fgets($file);
}
fclose($file);
var_dump($members);
?>
$lines = explode("n", file_get_contents('foo.txt'));
$lines = array();
while (($line = fgets($file)) !== false)
array_push($lines, $line);
$file = __DIR__."/file1.txt";
$f = fopen($file, "r");
$array1 = array();
while ( $line = fgets($f, 1000) )
{
$nl = mb_strtolower($line,'UTF-8');
$array1[] = $nl;
}
print_r($array);
<?php
$file = fopen("members.txt", "r");
$i = 0;
while (!feof($file)) {
$line_of_text .= fgets($file);
}
$members = explode("n", $line_of_text);
fclose($file);
print_r($members);
?>
$file = file("links.txt");
print_r($file);
$Names_Keys = [];
$Name = strtok(file_get_contents($file), "n");
while ($Name !== false) {
$Names_Keys[$Name] = 0;
$Name = strtok("n");
}
Use PHP to convert text file into array
<?php
$file="140724.txt";
$fopen = fopen($file, "r");
$fread = fread($fopen,filesize("$file"));
fclose($fopen);
$remove = "n";
split = explode($remove, $fread);
foreach ($split as $string)
{
echo "$string<br><br>";
}
?>
<?php
$file="140724.txt";
$fopen = fopen($file, 'r');
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = "n";
$split = explode($remove, $fread);
$array[] = null;
$tab = "t";
foreach ($split as $string)
{
$row = explode($tab, $string);
array_push($array,$row);
}
echo "<pre>";
print_r($array);
echo "</pre>";
?>
<?php
$myfile = fopen("test.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
$text[] = fgets($myfile);
}
fclose($myfile);
print_r($text);
?>
function tab_to_array($src='', $delimiter=',', $is_file = true)
{
if($is_file && (!file_exists($src) || !is_readable($src)))
return FALSE;
$header = NULL;
$data = array();
if($is_file){
if (($handle = fopen($src, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
}
else{
$strArr = explode("n",$src);
foreach($strArr as $dataRow){
if($row = explode($delimiter,$dataRow))
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
}
}
return $data;
}
/**
* Example for file
*/
print_r(tab_to_array('example.csv'));
/**
* Example for raw string
*/
$str = "name number
Lorem 11
ipsum 22";
print_r(tab_to_array($str, "t", false));
6 Ways to Read Files In PHP – Into String, Array, And More!
<?php
// (A) READ ENTIRE CONTENTS INTO A STRING
$text = file_get_contents("README.txt");
echo $text;
// (B) ALSO CAN FETCH FROM URL
$text = file_get_contents("https://en.wikipedia.org/wiki/Aha_ha");
echo $text;
<?php
// (A) FILE TO ARRAY
$array = file("README.txt");
print_r($array);
// (B) ADDITIONAL OPTION - SKIP EMPTY LINES
$array = file("README.txt", FILE_SKIP_EMPTY_LINES);
print_r($array);
<?php
$curl = curl_init("https://en.wikipedia.org/wiki/Aha_ha");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
echo $data;
<?php
// (A) OPEN FILE
$handle = fopen("README.txt", "r") or die("Error reading file!");
// (B) READ LINE BY LINE
while (($line = fgets($handle)) !== false) {
// To better manage the memory, you can also specify how many bytes to read at once
// while (($line = fgets($handle, 4096)) !== false) {
echo $line;
}
// (C) CLOSE FILE
fclose($handle);
<?php
// (A) START OUTPUT BUFFER
ob_start();
// (B) HTTP HEADERS TO FORCE DOWNLOAD
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename="README.txt"");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
header("Content-Length: ".filesize("README.txt"));
// (C) OUTPUT ALL THE HEADERS & STOP BUFFERING
ob_end_flush();
// (D) READ AND OUTPUT FILE DIRECTLY
readfile("README.txt");
exit();
<!DOCTYPE HTML>
<html>
<body>
<?php
require "README.txt";
?>
</body>
</html>
How to convert text file to array in php?
0 hbhenet pppoe D4:CA:6D:F1:5D:5A 10.23.0.254 8h1m36s
1 R moh@hj pppoe 00:27:22:16:3B:F2 10.17.0.253 8h1m36s
2 R omarmool@dr pppoe 68:72:51:3A:53:1B 10.17.0.251 8h1m36s
3 R admin@khr2 pppoe 24:A4:3C:9F:83:10 10.17.0.252 8h1m36s
4 R yas@ahmed.m pppoe 24:A4:3C:E6:DD:3E 10.17.0.250 8h1m36s
5 R hus@ahmed.m pppoe 44:D9:E7:DA:32:95 10.16.0.246 8h1m36s
$file="converted.txt";
$fopen = fopen($file, "r");
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = "n";
$split = explode($remove, $fread);
$array[] = NULL;
$tab = "t";
foreach ($split as $string)
{
$row = explode($tab, $string);
array_push($array,$row);
}
echo "<pre>";
print_r($array);
echo "</pre>";
Array
(
[0] =>
[1] => Array
(
[0] => 0 hbhenet pppoe D4:CA:6D:F1:5D:5A 10.23.0.254 8h1m36s
)
[2] => Array
(
[0] => 1 R moh@hj pppoe 00:27:22:16:3B:F2 10.17.0.253 8h1m36s
)
[3] => Array
(
[0] => 2 R omarmool@dr pppoe 68:72:51:3A:53:1B 10.17.0.251 8h1m36s
)
[4] => Array
(
[0] => 3 R admin@khr2 pppoe 24:A4:3C:9F:83:10 10.17.0.252 8h1m36s
)
[5] => Array
(
[0] => 4 R yas@ahmed.m pppoe 24:A4:3C:E6:DD:3E 10.17.0.250 8h1m36s
)
[6] => Array
(
[0] => 5 R hus@ahmed.m pppoe 44:D9:E7:DA:32:95 10.16.0.246 8h1m36s
)
)
[0] => Array
(
[0] => Array
(
[0] => 0
[1] =>
[2] => hbhenet
[3] => pppoe
[4] => D4:CA:6D:F1:5D:5A
[5] => 10.23.0.254
[6] => 8h1m36s
)
[1] => Array
(
[0] => 1
[1] => R
[2] => moh@hj
[3] => pppoe
[4] => 00:27:22:16:3B:F2
[5] => 10.17.0.253
[6] => 8h1m36s
)
[2] => Array
(
[0] => 2
[1] => R
[2] => omarmool@dr
[3] => pppoe
[4] => 68:72:51:3A:53:1B
[5] => 10.17.0.251
[6] => 8h1m36s
)
[3] => Array
(
[0] => 3
[1] => R
[2] => admin@khr2
[3] => pppoe
[4] => 24:A4:3C:9F:83:10
[5] => 10.17.0.252
[6] => 8h1m36s
)
[4] => Array
(
[0] => 4
[1] => R
[2] => yas@ahmed.m
[3] => pppoe
[4] => 24:A4:3C:E6:DD:3E
[5] => 10.17.0.250
[6] => 8h1m36s
)
[5] => Array
(
[0] => 5
[1] => R
[2] => hus@ahmed.m
[3] => pppoe
[4] => 44:D9:E7:DA:32:95
[5] => 10.16.0.246
[6] => 8h1m36s
)
)
)
0 hbhenet pppoe D4:CA:6D:F1:5D:5A 10.23.0.254 8h1m36s
1 R moh@hj pppoe 00:27:22:16:3B:F2 10.17.0.253 8h1m36s
2 R omarmool@dr pppoe 68:72:51:3A:53:1B 10.17.0.251 8h1m36s
3 R admin@khr2 pppoe 24:A4:3C:9F:83:10 10.17.0.252 8h1m36s
4 R yas@ahmed.m pppoe 24:A4:3C:E6:DD:3E 10.17.0.250 8h1m36s
5 R hus@ahmed.m pppoe 44:D9:E7:DA:32:95 10.16.0.246 8h1m36s
<?php
$array = explode("n", file_get_contents('myfile.txt'));
echo "<pre/>";print_r($array);
Array
(
[0] => 0 hbhenet pppoe D4:CA:6D:F1:5D:5A 10.23.0.254 8h1m36s
[1] => 1 R moh@hj pppoe 00:27:22:16:3B:F2 10.17.0.253 8h1m36s
[2] => 2 R omarmool@dr pppoe 68:72:51:3A:53:1B 10.17.0.251 8h1m36s
[3] => 3 R admin@khr2 pppoe 24:A4:3C:9F:83:10 10.17.0.252 8h1m36s
[4] => 4 R yas@ahmed.m pppoe 24:A4:3C:E6:DD:3E 10.17.0.250 8h1m36s
[5] => 5 R hus@ahmed.m pppoe 44:D9:E7:DA:32:95 10.16.0.246 8h1m36s
)
<?php
$array = explode("n", file_get_contents('myfile.txt'));
echo "<pre/>";print_r($array);
$new_array = array();
foreach($array as $key =>&$arr){
if ($arr[3] !=='R'){
$arr[3] ="_";
}
$parts = preg_split('/s+/', trim($arr));
if($parts[1] == '_'){
$parts[1] = '';
}
$new_array[$key] = $parts;
}
echo "<pre/>";print_r($new_array);
<?php
$array = explode("n", file_get_contents('myfile.txt'));
echo "<pre/>";print_r($array);
$new_array = array();
$indexed_array = Array('number','flag','name','service','mac','IP','uptime');
foreach($array as $key =>&$arr){
if ($arr[3] !=='R'){
$arr[3] ="_";
}
$parts = preg_split('/s+/', trim($arr));
if($parts[1] == '_'){
$parts[1] = '';
}
$new_array[$key] = array_combine($indexed_array,$parts);
}
echo "<pre/>";print_r($new_array);
<?php
$lines = array();
$fopen = fopen('converted.txt', 'r');
while (!feof($fopen)) {
$line=fgets($fopen);
$line=trim($line);
$lines[]=$line;
}
fclose($fopen);
$finalOutput = array();
foreach ($lines as $string)
{
$string = preg_replace('!s+!', ' ', $string);
$row = explode(" ", $string);
array_push($finalOutput,$row);
}
echo "<pre>";
print_r($finalOutput);
echo "</pre>";
?>
Array
(
[0] => Array
(
[0] => 0
[1] => hbhenet
[2] => pppoe
[3] => D4:CA:6D:F1:5D:5A
[4] => 10.23.0.254
[5] => 8h1m36s
)
[1] => Array
(
[0] => 1
[1] => R
[2] => moh@hj
[3] => pppoe
[4] => 00:27:22:16:3B:F2
[5] => 10.17.0.253
[6] => 8h1m36s
)
[2] => Array
(
[0] => 2
[1] => R
[2] => omarmool@dr
[3] => pppoe
[4] => 68:72:51:3A:53:1B
[5] => 10.17.0.251
[6] => 8h1m36s
)
[3] => Array
(
[0] => 3
[1] => R
[2] => admin@khr2
[3] => pppoe
[4] => 24:A4:3C:9F:83:10
[5] => 10.17.0.252
[6] => 8h1m36s
)
[4] => Array
(
[0] => 4
[1] => R
[2] => yas@ahmed.m
[3] => pppoe
[4] => 24:A4:3C:E6:DD:3E
[5] => 10.17.0.250
[6] => 8h1m36s
)
[5] => Array
(
[0] => 5
[1] => R
[2] => hus@ahmed.m
[3] => pppoe
[4] => 44:D9:E7:DA:32:95
[5] => 10.16.0.246
[6] => 8h1m36s
)
)
PHP – Read CSV File into an Array
Yohan, USA, 32 years old
Jean, United Kingdom, 25 years old
Alex, France, 22 years old
Emily, USA, 18 years old
<?PHP
function read($csv){
$file = fopen($csv, 'r');
while (!feof($file) ) {
$line[] = fgetcsv($file, 1024);
}
fclose($file);
return $line;
}
// Define the path to CSV file
$csv = 'myfile.csv';
$csv = read($csv);
echo '<pre>';
print_r($csv);
echo '</pre>';
?>
Array
(
[0] => Array
(
[0] => Name
[1] => Country
[2] => Age
)
[1] => Array
(
[0] => Alex
[1] => France
[2] => 22 years old
)
[2] => Array
(
[0] => Emily
[1] => USA
[2] => 18 years old
)
[3] => Array
(
[0] => Jean
[1] => United Kingdom
[2] => 25 years old
)
[4] => Array
(
[0] => Yohan
[1] => USA
[2] => 32 years old
)
)
How To Read A Text File Line By Line In PHP?
codespeedy.com
eyeswift.com
google.com
facebook.com
twitter.com
linkedin.com
<?php
if ($file = fopen("domainlist.txt", "r")) {
while(!feof($file)) {
$textperline = fgets($file);
echo $textperline;
}
fclose($file);
}
?>
<?php
if ($file = fopen("domainlist.txt", "r")) {
while(!feof($file)) {
$textperline = fgets($file);
echo " <a href='http://" .$textperline. "'>" . $textperline . "</a><br/><br/> ";
}
fclose($file);
}
?>
$file = fopen("domainlist", "r") //the .txt has removed
$DB = Get-Content $file
foreach ($Data in $DB) {
$First, $Second, $Third, $Fourth = $Data -split ',' -replace '^s*|s*$'
write-host "First is: "$First
write-host "Second is: "$Second
write-host "Third is: "$Third
write-host "Fourth is: "$Fourth
Write-Host ""
}
$DB = Get-Content $file
foreach ($Data in $DB) {
$First, $Second, $Third, $Fourth = $Data -split ',' -replace '^s*|s*$'
write-host "First is: "$First
write-host "Second is: "$Second
write-host "Third is: "$Third
write-host "Fourth is: "$Fourth
Write-Host ""
}
Previous PostNext Post