title: PHP - Find a string in file then show it's line number
body: <p>I have an application which needs to open the file, then find string in it, and print a line number where is string found.</p>
  
  <p>For example, file <strong>example.txt</strong> contains few hashes: <br/></p>
  
  <blockquote>
    <p>APLF2J51 1a79a4d60de6718e8e5b326e338ae533<br/> EEQJE2YX
    66b375b08fc869632935c9e6a9c7f8da O87IGF8R<br/>
    c458fb5edb84c54f4dc42804622aa0c5 APLF2J51<br/> B7TSW1ZE
    1e9eea56686511e9052e6578b56ae018<br/> EEQJE2YX
    affb23b07576b88d1e9fea50719fb3b7<br/></p>
  </blockquote>
  
  <p><br/>
  So, I want to PHP search for "1e9eea56686511e9052e6578b56ae018" and print out its line number, in this case 4.</p>
  
  <p>Please note that there are will not be multiple hashes in file.</p>
  
  <p>I found a few codes over Internet, but none seem to work.</p>
  
  <p>I tried this one:</p>
  
  <pre><code>&lt;?PHP
  $string = "1e9eea56686511e9052e6578b56ae018"; 
  $data   = file_get_contents("example.txt"); 
  $data   = explode("\n", $data); 
  for ($line = 0; $line &lt; count($data); $line++) { 
  if (strpos($data[$line], $string) &gt;= 0) { 
  die("String $string found at line number: $line"); 
  } 
  } 
  ?&gt;
  </code></pre>
  
  <p>It just says that string is found at line 0.... Which is not correct....</p>
  
  <p>Final application is much more complex than that...
  After it founds line number, it should replace string which something else, and save changes to file, then goes further processing....  </p>
  
  <p>Thanks in advance :)</p>