#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    How to print few lines by selecting a particular string in php


    Hi,

    I am new in PHP5 and want to use it for extracting some lines from a text file. For that purpose, I have selected a particular string and want to print succssive three lines from this information. I have tried with a small code which is given below.

    #!/usr/bin/php5
    <?php
    $file = fopen("e-coli-bl21.gb","r");
    $read = 0;
    while($line = fgets($file))
    #echo fgets($file);
    {
    //keep going til you get to features
    if(!$read)
    {
    if(strstr($line,"FEATURES"))
    {
    $read = 1;
    echo $line.PHP_EOL; /*here I need succssive three lines when the string 'FEATURES' has been selected. But I am getting only those lines which contain the string 'FEATURES' in my program*/

    }
    continue;
    }
    if(strstr($line,"gene "))
    {
    # echo $line;
    }
    }

    Can anyone rectify me?

    Best
    Sudipta
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    1) Please enclose your code in [ PHP ] tags. See the sticky at the top of this forum.
    2) It is not clear what you are asking. If you find the string"FEATURES" are you saying you want to print the next 3 lines, print that line and the next 2 or something different?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    How to print the next two lines after finding the string 'FEATURES'?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    You mean something like this:
    PHP Code:
    if(strstr($line,"FEATURES")) {
       echo 
    "$line<br />";
       echo 
    fgets($file)."<br />";
       echo 
    fgets($file)."<br />";

    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by gw1500se
    You mean something like this:
    PHP Code:
    if(strstr($line,"FEATURES")) {
       echo 
    "$line<br />";
       echo 
    fgets($file)."<br />";
       echo 
    fgets($file)."<br />";

    Thank you very much. My problem has been solved for now.

IMN logo majestic logo threadwatch logo seochat tools logo