By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

search text file

P: 44
Hi everyone i want to search the text file,using pregmatch() but it seems like
it dose't serch.maybe my serching pattern is not right.look at the following code.

Myfile look like this
--------------------------------
file : (49/06/01) [2007] simon 13;
file : (68/05/01) [2007] John 17 ;

i want to serch [2007] simon 13 on the text file.
can anyone help me with pregmatch patterns.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $jaar = $_POST[year];
  3. $name = $_POST[name];
  4. $empno = $_POST[employeeno];
  5. $file =$q."Myfile.txt"; // q =path to myfile
  6. $toSearch = "[".$jaar."]"." ".$name." ".$empno;
  7. $lines = file($file); 
  8.  
  9. $count;
  10. $lineNumbers;
  11.  
  12. if ($lines)
  13.  
  14. {
  15.  
  16.    foreach($lines as $lineNumber) 
  17.  
  18.     { 
  19.     $lineNumbers++;
  20.  
  21.         $foundAMatch = preg_match("/$toSearch/i", $lineNumber, $matches, PREG_OFFSET_CAPTURE); 
  22.  
  23.         if ($foundAMatch > 0) 
  24.  
  25.             { 
  26.  
  27.                    $found = $matches[0][0]; 
  28.  
  29.                $count=$lineNumber;
  30.                         echo $lineNumber."<br />";
  31.  
  32.  
  33.             }         
  34.  
  35.         }    
  36.  
  37.         //$str_array = explode(" ",$count);
  38.  
  39.         //echo $str_array[0]."<br/>";
  40.  
  41.  
  42.  
  43.     }
  44.  
  45.     else
  46.  
  47.     {
  48.  
  49.         $line="no file/folder found";
  50.  
  51.         echo $line;
  52.  
  53.     }
  54. ?>
Aug 8 '08 #1
Share this Question
Share on Google+
1 Reply


rohypnol
P: 54
Try this:

$toSearch = "\\[{$jaar}\\] {$name} {$empno}";
Aug 8 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.