...

View Full Version : simplest way to match



matak
11-27-2007, 01:14 PM
for some reason i can't think straight today. i need to match date in this array


Array
(
[0] => .
[1] => ..
[2] => 2007-10-30127.0.0.1
[3] => 2007-10-31127.0.0.1
[4] => 2007-11-02127.0.0.1
[5] => 2007-11-14127.0.0.1
[6] => 2007-11-15127.0.0.1
[7] => 2007-11-16127.0.0.1
[8] => 2007-11-19127.0.0.1
[9] => 2007-11-27127.0.0.1
)

it's script to show visits today, and i need to match ALL today dates with all dates in array to get the number of visits today.
what function to use? preg_match, or something else?
thanks

fl00d
11-27-2007, 01:42 PM
I would assume array_search (http://ca.php.net/manual/en/function.array-search.php)
would be the easiest way to search.

matak
11-27-2007, 01:47 PM
array search matches the whole string. hmm, can i use regular expressions with array search? :)

shyam
11-27-2007, 03:33 PM
array search matches the whole string. hmm, can i use regular expressions with array search? :)

use array_filter (http://ca.php.net/manual/en/function.array-filter.php) and use a regex inside the callback

matak
11-27-2007, 04:55 PM
uber cool function. i've been searching through array functions, and somewhere else i also found using functions in array. that's super duper cool new feature of PHP that i lately discovered. awesome :)

aedrin
11-27-2007, 05:03 PM
It sounds like this should be in a database.

matak
11-27-2007, 05:53 PM
No it's flat file. Files are saved in folder, counted for visits, and searched by date for visits today :)


<?php
function getVisits($variable) {

$visits = array();

$date = date('Y-m-d');

if ($handle = opendir('baza/stats/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
array_push($visits, $file);
}
}
closedir($handle);
}
$key = array_search($date.$variable, $visits);

if ($key !== FALSE) {

echo "<p>Posjeta do sada: ".count($visits)."</p>";

}
elseif ($key === FALSE){
$file = fopen("baza/stats/".$date.$variable, 'a');
fwrite($file, $variable);
fclose($file);

getVisits($variable);
}
}

function visit() {

$files = array();

$file1 = "";

$dir = opendir('baza/stats/');

while (FALSE !== ($file = readdir($dir))) {
array_push($files, $file);
$file1 .= $file;
}
closedir($dir);

$date = date('Y-m-d');


preg_match_all ("/$date/", $file1, $string);

print "<p>Visits Today: ".count($string[0])."</p>";
}
?>

i changed from array to string, and then just matched the days, it was simpler and i think it's faster then searching through array. $files array is not needed for this script, i was just practicing something there... $variable is users ip of course.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum