Mega Code Archive

 
Categories / Php / Statistics and Counters
 

Text Based Counter that formats output

<?php // Only needed for under php5 use: function str_split($the_string, $the_number) { $startoff_nr = 0; $the_output_array = array(); for($z = 1; $z < ceil(strlen($the_string)/$the_number)+1 ; $z++) { $startoff_nr = ($the_number*$z)-$the_number; $the_output_array[] = substr($the_string, $startoff_nr, $the_number); } return($the_output_array); } // Hit file (chmod a+rw to it) $count_my_page = ("/home/ytslfazk/public_html/dotblog/hits.txt"); // Read contents $hits = file($count_my_page); // Increase hits $hits[0]++; // Open for write $fp = fopen($count_my_page , "w"); // Save new value fputs($fp , "$hits[0]"); // Close file fclose($fp); // Explained: It's reversed so we can split it into 3's // and not have 3400 turn into 340,0 $hits_expanded = str_split(strrev($hits[0]), 3); for ($i = 0; $i <= sizeof($hits_expnaded) + 1; $i++) { if (isset($hits_string)) // Set the string back to normal. // NOTE: The newly added number goes _INFRONT_ of the old one because it // was reversed. $hits_string = strrev($hits_expanded[$i]).','.$hits_string; else // Set the string back to normal $hits_string = strrev($hits_expanded[$i]); } echo '<b>'.$hits_string.'</b>'; ?>