Finding a string inside a file

Discussion in 'PHP' started by navbeacon, Oct 13, 2008.

  1. #1
    
    $file = file_get_contents("myfile.txt");
      $needle = "needle";
    
        $countfile = substr_count($file, $needle);
    
      echo $countfile;
    
    Code (markup):
    This PHP code echoes 0 when I try to run it even though myfile.txt contains needle. Anyone know why? I guess I overlooked something obvious.

    myfile.txt:

    
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer ullamcorper sapien ac tellus. Vivamus euismod. 
    
    needle 
    
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer ullamcorper sapien ac tellus. Vivamus euismod. 
    
    Code (markup):
    EDIT: Disregard this topic, I used $file within something that already used the variable (a folder tree), thus is came out empty.
     
    navbeacon, Oct 13, 2008 IP
  2. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    maybe the filename is incorrect, thats why there is no text getting in variable $file and showing 0 in substr_count.
     
    mehdi, Oct 13, 2008 IP
  3. navbeacon

    navbeacon Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nope, I tried echoing $file and it printed the file without errors. :/

    EDIT: Disregard this topic, I used $file within something that already used the variable (a folder tree), thus is came out empty.
     
    navbeacon, Oct 13, 2008 IP
  4. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Have you tried using another variable rather then $file?
     
    mehdi, Oct 13, 2008 IP