Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts

    simplest way to match

    for some reason i can't think straight today. i need to match date in this array

    Code:
    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

  • #2
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    I would assume array_search
    would be the easiest way to search.

  • #3
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    array search matches the whole string. hmm, can i use regular expressions with array search?

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by matak View Post
    array search matches the whole string. hmm, can i use regular expressions with array search?
    use array_filter and use a regex inside the callback
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #5
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    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

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    It sounds like this should be in a database.

  • #7
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    No it's flat file. Files are saved in folder, counted for visits, and searched by date for visits today

    PHP Code:
    <?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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •