35 users online (0 members and 35 guests)  


  Results 1 to 5 of 5

Related

  1. Replies: 1
  2. Extracting Form Field Values    Forum: HTML Forum
    Replies: 0
  3. Writing to text file problem    Forum: CGI Perl Forum
    Replies: 5
  4. Replies: 1
  5. Email Text File    Forum: CGI Perl Forum
    Replies: 1
  1. #1
    hottac's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2009
    Posts
    3

    extracting comments

    Hi all, I know this probably an elementary question to some of you, but I'm just learning php myself.

    I was wondering if someone could help me with a script to extract a comment line from a text file. I have alot of txt files handed to me at work and all of them have comments that I need to extract and combine into one txt file. Lets say that one txt file has bunch of sentence in it but usually at the end, it'd have a "Comment: brad does a great job of helping me" Is there a way where I could just drop all of the text files into a folder and let php extract all the comments and combined into one file? Thank you and any help would be very appreciated
    Last edited by hottac; 11-06-2009 at 07:53 PM.

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: extracting comments from text file

    Are the text files always formatted the same way? For example, are the comments always last and do they always begin with "Comment:"?

  3. #3
    hottac's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2009
    Posts
    3

    Re: extracting comments from text file

    yes, the text files are always formatted the same. For example:

    something line
    something line
    Comment:brad does a great job of helping me and it can go on and wrap itself
    something line

    Thanks

  4. #4
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: extracting comments from text file

    Use the following code, and modify it to do what you need. I have already modified it some for you.

    PHP Code:
    <?php
    $handle 
    = @fopen("/tmp/inputfile.txt""r");
    if (
    $handle) {
        while (!
    feof($handle)) {
            
    $buffer fgets($handle);
            if (
    strtolower(substr($buffer08)) == "comments") {
                echo 
    $buffer;
            }
        }
        
    fclose($handle);
    }
    ?>
    You can also check php.net for more information on the above functions

  5. #5
    hottac's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2009
    Posts
    3

    Re: extracting comments from text file

    oh nice jthayne, I will give it a try during my time off this holiday. Lets say if I need it to modify where the script looks inside a folder and open all the text files in the folder and extract any comments, will that be hard to integrate into this script you made? Thank you so much for your time and if I don't hear from you, have a nice thanksgiving.



Tags for this Thread