WEB whats wrong with this PHP?

Discussion in 'OT Technology' started by MSTRMOO, Mar 7, 2009.

  1. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    Code:
    <?php
    
    $gnrlLogFile = "log.txt";
    $fh = fopen($gnrlLogFile, 'r');
    $logData = fread($fh, filesize($gnrlLogFile));
    fclose($fh);
    
    $word= "Mar" ;
    
    $string = $logData ;
    
    $occurences=(int) 0;
    
    preg_replace ( '`\b'.$word.'\b`ie' , '$occurences++;' , $string ) ;
    
    echo $occurences;
    ?> 
    im trying to count the number of times "Mar" appears in the log file :hs:
     
  2. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    got it. lol
     
  3. sonicsuby

    sonicsuby New Member

    Joined:
    Mar 15, 2000
    Messages:
    21,338
    Likes Received:
    0
    Location:
    CA
    it should be:

    if preg_match ($word, $string) { $occurrences++; }

    What you're doing would substitute $word with $occurrences in the string.
     

Share This Page