PHP count row after specific row on text file - php

I would like to count rows of text file (.txt) after specific row.
Example: I want after row 10 then row 11 until end will be count.
Is it possible?
What I've tried:
$files = "log.txt";
echo count(file($files));
But as you know it will count all rows.
What I want is, after row 10 then row 11 until end will be count.
Any idea?
UPDATE
Need to count only row with values, ignore for empty row/line

Try this
$files ="log.txt";
$f = fopen($files, 'rb');
$row = 1;
while (!feof($f)) {
$row += substr_count(fread($f, 8192), "\n");
}
fclose($f);
echo $row;
this if empty line is not counted
$r = array_filter(array_map("trim", file($files)), "strlen");
echo count($r);

Similar to markatonay but shorter:
$text = file_get_contents('/tmp/log.txt');
$lines = array_filter(array_map('trim', explode("\n", $text)));
print_r($lines);
echo "COUNT:" . count($lines);
echo "\r\n";

Related

PHP script to go through each line of CSV and use IF statement

I am new to PHP and am trying to create a script that goes through a CSV.
For each row (excluding the headers), checks to see if column 2 and 3 (total rows being 0,1,2,3) when combined, are greater or equal to 1; then display a "1" in column 1. If column 2 and 3 are less than 1, then display "0" in column 1.
An example of the CSV is displayed below:-
sku,is_in_stock,warehouse_3,warehouse_4
AP-STYLUS,1,20,5
RC-3049,0,0,0
NFNC-FLAT-CAP,1,20,20
NFNC-HOOD14-ZIP-S,1,0,5
How can this be done?
You need to replace file.csv with the real filename.
<?php
$str = file_get_contents("file.csv");
//$str = "sku,is_in_stock,warehouse_3,warehouse_4
AP-STYLUS,0,20,5
RC-3049,0,0,0
NFNC-FLAT-CAP,0,20,20
NFNC-HOOD14-ZIP-S,1,0,5";
$arr = explode("\n", $str);
$result = array();
Foreach($arr as $line){
$linearr = explode(",", $line);
if(is_numeric($linearr[2])){
if($linearr[2]+$linearr[3]>=1){
$linearr[1]="1";
$line = implode("," , $linearr);
}else{
$linearr[1]="0";
$line = implode("," , $linearr);
}
}
$result[]=$line;
}
$newstr = implode("\n", $result);
file_put_contents("file.csv", $newstr);
?>
Edit, sorry forgot about the "0" part.
https://3v4l.org/63vmh

add a column with increasing value for each csv line (php)

i have a csv file with values,
i want to add a column ID with an increasing number for each row.
I see this page: [http://www.w3schools.com/php/func_filesystem_fputcsv.asp]
in this case i have an array defined but i want first count the row of the csv, than for each row add a column with a value.
Anybody can suggest me a way?
<?php
$list = array
(
"Peter,Griffin,Oslo,Norway",
"Glenn,Quagmire,Oslo,Norway",
);
$file = fopen("contacts.csv","w");
$i = 0;
foreach ($list as $line)
{
$newline = $line . "," . $i;
fputcsv($file,explode(',',$newline));
$i++;
}
fclose($file); ?>

count rows of a file with same first character

In a comma separated csv file, I want to count the number of rows only where the the first number is same.
Following is the example data, I want to get number of rows which start with 2 (2 rows), and the rows which start with 4 (3 rows). This is just an example, the numbers are random.:
2,0,0
2,1,0
4,0,0
4,3,0
4,4,0
I'm trying following code, I can count only all rows of the file but do not know how can I count only the rows which have same first number.
$i = 0;
while ($i < 5) { //fixed number of times
$i++;
$rows = 0;
$fp = fopen("test.csv", "r");
while (fgetcsv($fp)) { //don't want all rows
$rows++;
}
fclose($fp);
echo $rows;
}
Edit:
Sorry, I forgot to mention the numbers in above file are random, they are not always 2 or 4.
You will need an array to get statistics.
$i = 0;
$agg = []; // to count stats
while ($i < 5) {
$i++;
$rows = 0;
$fp = fopen("test.csv", "r");
while ($line = fgetcsv($fp)) {
$number = $line[0]; // get first number
if(isset($agg[$number])){ // if there is the number in stats
$agg[$number]++; // count new one
} else {
$agg[$number] = 1; // mark as found one
}
}
fclose($fp);
print_r($agg);
}
You could probably try something like this :
$i = 0;
while ($i < 5) { //fixed number of times
$i++;
$rows = array();
$fp = fopen("test.csv", "r");
while ($data = fgetcsv($fp)) {
$first = $data[0];
if(isset($rows[$first]) {
$rows[$first] += 1;
} else {
$rows[$first] = 1;
}
}
fclose($fp);
print_r($rows);
}
I didn't test my code, so it may contains errors.
After the execution, $rows will contain an associative array with the form 'first number' => 'count'
What I don't understand however is why you are doing it five times and also why you are not using a for loop for that instead of a while ?
Assuming you know how to get the contents of the text file, something like this should work:
$ar = explode("\n",$txtfileContents);
$counts = array();
foreach($ar as $row){
$counts[$row[0]]++;
}
$counts now contains an array of all first characters and how many times they appeared. You can now simply access them like this:
echo "'2' appeared ".$count['2']." times<br/>";
echo "'4' appeared ".$count['4']." times";
Step by step:
Split the text file into an array of individual rows:
$ar = explode("\n",$txtfileContents);
(Try alternatively file() here, which may be more reliable than explode)
Loop through it:
foreach($ar as $row){
}
Then check the first character inside the loop and count each character separately:
$counts[$row[0]]++;

Call a single cell from a .CSV using PHP

I've a problem to import a single cell in csv,
i have a csv file that contain 7 column and ~1420 row,
i need to read only the 4 column number in 1420's row, how can i do it?
<?php
$url = "wp-admin/ftp/test.csv";
$csvData = file_get_contents($url);
$lines = explode("\n", $csvData);
$array = array();
foreach ($lines as $line) {
$array[] = str_getcsv($line);
}
echo $array[1];
?>
with this i have an array to output with all cell in one row,
but i need to take the third number in that row, and not all of it.
I believe $array is multidimensional the following should print what you want:
echo $array[1419][3];
Otherwise you can print it to check how it is structured:
print_r($array);
UPDATE: unidimensional
foreach ($lines as $line) {
$array[] = str_getcsv($line)[3];//PHP 5.4
}
echo $array[1419];

Outputting a random record from csv?

I have a csv file that contains 6 columns. I want to return one record (not the whole row) at random into my web page. For example, from row 5 return column 1 and 4.
The code below returns the whole file (columns 1, 2, 3). I only want one row returned. How do I modify this code to bring back one record?
<?PHP
$csvfile = "table.csv";
$file_handle = fopen($csvfile, "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";
}
fclose($file_handle);
?>
Using your code above this is how you could potentially modify it to show a particular row and column. As #Dagon mentioned in his post you could use array_rand for random entries (see below). You could also specify the row and column positions in an array if you like.
<?php
$csvfile = "table.csv";
$file_handle = fopen($csvfile, "r");
$line_of_text = array();
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
// Random Row and Column
$random_row = array_rand($line_of_text);
$random_column = array_rand($line_of_text[$random_row]);
echo $line_of_text[$random_row][$random_column];
//Specified Row 5 Column 1
$row = 5;
$column = 1;
echo $line_of_text[$row-1][$column-1];
?>
inefficient, however you could change print to
$p[]= ...
then after the loop
echo $p[array_rand($p)];

Categories