Read From a File

Discussion in 'PHP' started by qualityhostings, Mar 19, 2009.

  1. #1
    Hello

    I need to search a file and return the values

    Example

    The file

    abc.txt

    
    <master:vivek>
    
    <reseller:vivek11>
    <reseller:vivek22>
    <reseller:vivek33>
    
    
    Code (markup):

    Ok, What I want is , to read this file and get the values , vivek, vivek11,vivek22, and vivek33


    Like

    $master = "vivek"

    $reseller[0] = "vivek11"
    $reseller[1] = "vivek22"
    $reseller[2] = "vivek33"


    Can anybody help me ?

    Thank you
     
    qualityhostings, Mar 19, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $lines = file('abc.txt');
    foreach ($lines as $line)
    {
       if (preg_match('/<master:([^>]+)/i', $line, $matches))
          $master = trim($matches[1]);
       if (preg_match('/<reseller:([^>]+)/i', $line, $matches))
          $reseller[] = trim($matches[1]);
    }
    Code (markup):
     
    SmallPotatoes, Mar 19, 2009 IP
  3. Ilyes

    Ilyes Banned

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Or,
    
    $lines = file('abc.txt');
    foreach ($lines as $line)
    {
       if ($line)<>""){
    $arr = explode(":",$line);  // split by  : 
    $arr[0]=substr($arr[0],2,strlen($arr[0])-1); // delete <
    $arr[1]=substr($arr[1],1,strlen($arr[1])-1); // delete >
    
    if ($arr[0] == "master" ){
     echo 'master = "' . $arr[1] ;
    }elseif ($arr[0] == "reseller" ){
    .......
    
     }    
    }
    
    PHP:
    You can cmplete, I just give the idea :)
     
    Ilyes, Mar 19, 2009 IP