How can i search in a txt file?

Discussion in 'PHP' started by teque, Apr 19, 2010.

  1. #1
    Its possible to search through a txt file?
    I made a php script that open a txt file and writes on it but now i want to be able to search through the file and find whats it on it.
    The txt file looks kind of like this

    
    .........
    ctl00$ctl00$cpMain$cpMain$Box$firs_name=THEIR_NAME
    ctl00$ctl00$cpMain$cpMain$Box$last_name=THEIR_LAST_NAME
    .....
    
    Code (markup):
    What i want to do its that when the user search for their first name it will show them their last name. Its just an example.

    Sorry for my bad english.
     
    teque, Apr 19, 2010 IP
  2. job0107

    job0107 Peon

    Messages:
    23
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If your text file is laid out exactly the way you have it displayed (two lines for each record) then you could do it like this.

    Example text file called test.txt.
    
    ctl00$ctl00$cpMain$cpMain$Box$firs_name=THEIR_NAME
    ctl00$ctl00$cpMain$cpMain$Box$last_name=THEIR_LAST_NAME
    
    Code (markup):
    The script:
    
    <?php
    $firstName = "THEIR_NAME";
    $theFile = "test.txt";
    $fp = fopen($theFile,"r");
    while(!feof($fp))
    {
     $line1 = explode("=",fgets($fp));
     $line2 = explode("=",fgets($fp));
     if(trim($line1[1]) == $firstName)
     {
      echo "First Name = ".$line1[1]."<br />
            Last Name = ".$line2[1]."<br />";
      }
     }
    ?>
    
    PHP:
     
    job0107, Apr 19, 2010 IP
  3. devidhood

    devidhood Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hiiii
    hey u can get all inframtion abt php script in w3schools site it realy very helpful.u can get all kind f script and help

    i hope this will help u

    thanks
    Devid
     
    devidhood, Apr 20, 2010 IP