How to count different lines? So I mean if I have "something.txt" and there is 10 lines where is four lines printed "X" and six lines printed "B" it should count "2". Understand? Thanks!
if file not big, then <? $lines = file('something.txt'); sort($lines); $cnt = count($lines); $res=($cnt)?1:0; for($i=1; $i<$cnt; $i++) { if ($lines[$i]!=$lines[$i-1]) $res++; } echo $res; ?> it's idea, didn't tested it though, but must work
just an idea read the file with file command $lines = file('something.txt'); PHP: now the all line are in the $lines array let's say the value of array look like this lines=array("Mango","Banana","Orange","Banana"); $new_array=array_unique($lines); PHP: Remove the duplicate lines with unique_array function Now our $new_array will contain values ("Mango","Banana","Orange") $unique_lines = count($new_array); PHP: $unique_lines is What you require
Thank to you guys! Ravish´s idea will work. I have also another question which will make my script a lot easier.. Is there possibility to count only something from lines in "something.txt"? I mean I have all of the lines first "X" or "B" or "C" and then comes " - " and the line continues. So one line looks like: X - something... Can I count everything what comes before " - " ? Thanks!
I made a code but it doesn´t work. <?php $file = "recordlist.txt"; $i = 0; while ($i <= count($file) - 1 ) { $pum = explode(" - ", $file[$i]); $bands = file($pum[1]); $bands_array = array_unique($bands); $unique_bands = count($bands_array); $lines = count(file($file)); print "CDs: $lines<br /><br />"; echo "Different bands: $unique_bands<br /<br />"; $i++; } $list = file('recordlist.txt'); sort($list); for ($i=0; $i <= $lines; $i++) echo $list[$i]."<br \>"; ?> It prints just "1" for $unique_bands Can you help me guys? Thanks a lot!
this seems confusing to me Please put the file contents and let me know what the script to do. I can write one quick for you and will be explaining what it is doing at every step Ravish
I have a code with my CDlist. (It's not the one above) It makes strings to recordlist.txt file like this: Iron Maiden - 1981 - Killers Iron Maiden - 1982 - The Number of the Beast Judas Priest - 1990 - Painkiller Motörhead - 1979 - Overkill etc etc So. Counting how much CDs I have (lines in the recordlist.txt) is easy, but also I need to count how many different artist there is! It works if I $_POST["artist"] also to other .txt file (bands.txt) and count unique lines from there. But I think there should be easier way which counts unique lines before first " - " in recordlist.txt. Thanks to you Ravish, hope you understand what I mean
It would probably be easier to load the file contents into a string and use regular expression to get all artist names. preg_match_all('/(?:^|\n)([^-]+)/', $file_contents, $artists); // Different artists echo sizeof(array_unique($artists[1])); PHP: