getting specific lines from text file

Discussion in 'PHP' started by ironmankho, Jul 31, 2012.

  1. #1
    i want to get these lines

    [lnk]my data1[/lnk]
    [lnk]my data2[/lnk]
    [lnk]my data3[/lnk]
    [lnk]my data4[/lnk]
    [lnk]my data5[/lnk]
    [lnk]my data6[/lnk]
    [lnk]my data7[/lnk]
    [lnk]my data8[/lnk]
    [lnk]my data9[/lnk]
    [lnk]my data10[/lnk]


    form text file what method should i use
    i know that
    $data=file_get_contents('myfile.txt');

    assume this is text file
    myfile.txt

    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    [lnk]my data1[/lnk]
    [lnk]my data2[/lnk]
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining 
    [lnk]my data3[/lnk]
    [lnk]my data4[/lnk]
    [lnk]my data5[/lnk]
    [lnk]my data6[/lnk]
    [lnk]my data7[/lnk]
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining 
    [lnk]my data8[/lnk]
    [lnk]my data9[/lnk]
    [lnk]my data10[/lnk]
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    HTML:
     
    Solved! View solution.
    ironmankho, Jul 31, 2012 IP
  2. flotwig

    flotwig Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #2
    $lines = explode("\n",$data);
    Code (php):
    Then just run that through a foreach loop and check the substrs.
     
    flotwig, Jul 31, 2012 IP
  3. #3
    A slightly simpler way would be to grab the contents of all the tags with preg_match_all:

    preg_match_all('/\[lnk\](.*)\[\/lnk\]/', $data, $matches);
    PHP:
    With this, the contents of all the [lnk] tags will be pulled into $matches[1] as an array.
     
    Thorlax402, Aug 1, 2012 IP