The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 10 of 10

Thread: Finding line of keyword in a text file.

  1. #1

    Finding line of keyword in a text file.

    Hello! I am building a description system that will be implemented into a gallery script. The script will get the name of the image, find that name in a text file, and read all the text on the same line of the name of the image.

    I already have a script that reads lines from text files but now I need some help with the finding the line part. I need it to find the keyword and tell me the line it's on. I know strpos() can find the keyword for me but that tells me the amount of characters between the beginning of the file and the position of the keyword. So if anyone has any suggestions or example scripts that'd be awesome.

    Thanks for the help!

  2. #2
    SEE UPDATED VERSION IN POST #6
    whipped this up real quick. works like a charm.
    PHP Code:
    <?php
    /* find this needle... */
    $needle 'foo';

    /* ... in this haystack (from this text file)*/
    $haystack file_get_contents'my_text_file.txt' );

    /* see if the match was found for 'foo' */
    $line findLine$needle$haystack );
    if( 
    $line ) {
        
    print_r$line ); // print array contents out
        
    echo $line['needle'];    // foo
        
    echo $line['lineNum'];    // line where 'foo' was found
        
    echo $line['lineData'];    // data in line where 'foo' was found
        
    echo $line['strpos'];    // position in line where 'foo' was found
    }
    else {
        echo 
    'No match found for '.$needle;
    }



    /* returns the line number and line data in an array */
    function findLine$needle$haystack ) {
        
        
    /* split string at line breaks */
        
    $haystack_array split"\n"$haystack );
        
        
    /* loop through array contents */
        
    foreach( $haystack_array as $key=>$val ) {
            
    $strpos strpos$val$needle );
            
            
    /* return first match */
            
    if( $strpos ) {
                return array(
                        
    'needle'    => $needle,
                        
    'lineNum'     => $key,
                        
    'lineData'    => $val,
                        
    'strpos'    => $strpos
                        
    );
            }
        }
        
        
    /* if no matches, return FALSE */
        
    return FALSE;
    }

    ?>
    Last edited by bwh2; February 16th, 2007 at 02:18 AM.

  3. #3
    Can I see the text file you tested this on? I'm having no luck and I think it might the way I'm formatting mine.

  4. #4
    Code:
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc ut erat. In malesuada pede vel purus. 
    Nam volutpat nisi quis eros. Nunc quis erat. Donec dignissim lectus. Donec vel leo nec libero sagittis volutpat. Aliquam nisl. Vivamus volutpat risus ac nibh. Pellentesque eu est ut nisi scelerisque pharetra. Cras posuere nibh eget ante. Cras dictum ante quis leo. Fusce vitae lacus. Duis non nisl. Nullam dignissim, nibh quis ornare mollis, nulla velit congue enim, et facilisis metus sem at arcu. Aenean enim. Sed rutrum pretium orci. Sed sit amet enim at nunc porta posuere. Nunc mi tortor, aliquam id, porta a, egestas in, ipsum. Suspendisse ultricies leo at quam. Aliquam erat volutpat.
    Fusce quis dui volutpat dolor luctus condimentum. 
    Curabitur tincidunt blandit tortor. 
    Donec ultricies. Duis venenatis tortor vitae diam. Nam a orci vitae arcu scelerisque aliquam. Quisque imperdiet enim at dolor. In quam est, varius et, placerat vitae, varius vitae, purus. In aliquam diam consequat quam. Nullam non velit. 
    Phasellus lacinia imperdiet magna.
    Nulla scelerisque tempor urna. Donec ut eros at arcu hendrerit fermentum. 
    Etiam sodales lacinia tellus. Nam mollis varius massa. 
    Nam sem sapien, eleifend in, pulvinar sed, varius id, magna. Praesent blandit dolor eu lorem sodales rutrum. 
    Donec vel urna vel tortor suscipit consectetuer. Praesent quis tortor. Sed dui erat, malesuada quis, dignissim a, sodales ut, diam. Fusce egestas. Etiam tristique erat et nisi. Phasellus suscipit aliquet quam. Curabitur eu orci. Nunc arcu. In porttitor venenatis nunc. Donec est tortor, commodo ut, tincidunt eget, pretium ac, diam. Aliquam erat volutpat.
    you can replace 'foo' in my example with any word in the above text. and if you're on a mac, you might want to change "\n" in my function to "\r\n"

  5. #5
    Oh, I found what the problem was. You can't have it be the first word on the line.

    Thanks a million for the script.
    Last edited by Saurdo; February 16th, 2007 at 02:09 AM.

  6. #6
    whoops. i fixed that bug:
    PHP Code:
    <?php
    /* find this needle... */
        
    $needle 'foo';

    /* ... in this haystack */
    $haystack file_get_contents'my_text_file.txt' );

    /* see if the match was found for 'foo' */
    $line findLine$needle$haystack );
    if( 
    $line ) {
        
    print_r$line ); // print array contents out
        
    echo $line['needle'];    // foo
        
    echo $line['lineNum'];    // line where 'foo' was found
        
    echo $line['lineData'];    // data in line where 'foo' was found
        
    echo $line['strpos'];    // position in line where 'foo' was found
    }
    else {
        echo 
    'No match found for '.$needle;
    }



    /* returns the line number and line data in an array */
    function findLine$needle$haystack ) {
        
        
    /* split string at line breaks */
        
    $haystack_array split"\r\n"$haystack );
        
        
    /* loop through array contents */
        
    foreach( $haystack_array as $key=>$val ) {
            
    $strpos strpos$val$needle);
            
            
    /* return first match */
            
    if( $strpos === FALSE ) {
                
    next;
            }
            else {
                return array(
                        
    'needle'    => $needle,
                        
    'lineNum'     => $key+1,
                        
    'lineData'    => $val,
                        
    'strpos'    => $strpos
                        
    );
            }
        }
        
        
    /* if no matches, return FALSE */
        
    return FALSE;
    }

    ?>
    also note, that in this most version i made the lineNum returned the actual line number (not the array key). also note that this is case sensitive.

  7. #7
    Alright, Now I'm having a problem. Instead of finding the line it just displays the whole thing. I made no changes to the actual script and it still works when I don't use it with my script.

    Here's the code for the section I'm using it at:

    PHP Code:
    $line findLine"-".$slide.":"$gallery_root.$currentdir."desc.txt" );
    if( 
    $line ) {
        
    $lineDataSplit split("-".$slide.":"$line['lineData']);
        echo 
    "<h3>Detailed Description:</h3> </b>".$lineDataSplit[1]."<br>";
    }
    else {
        echo 
    '';
    }; 
    Where $slide equals the name of the image I am searching for.

    Thanks for any help!

    Edit:Actually, it seems to be the way I'm making my text file. My text file is located here if anyone wants to take a stab at finding out what's wrong with the formatting. I should mention I made the lines manually in PHP.

    Double Edit: I fixed it by opening it in a text editor, deleting the lines and recreating them. Why doesn't it like my PHP made lines?
    Last edited by Saurdo; February 18th, 2007 at 11:10 PM.

  8. #8

  9. #9
    Yes, I am. I just put it inside the function itself.

    Also, it's working on a text file when the script isn't in my gallery and not working on the same text file when it is in my gallery. This is confusing me...

    Edit: What the hell, it's working now. Maybe my host is doing something. I need a break, I'll take a look at it later and if the problem returns I'll post here.

    Thanks.
    Last edited by Saurdo; February 18th, 2007 at 11:22 PM.

  10. #10
    Alright, I figured out what was causing all my problems. I use the text and script editor "Notepad++" and for some reason, which I haven't found out yet, it uses a different method for adding new lines and when you open it in regular notepad you get little squares(if i try pasting the square into the forum editor it makes a new line). It only uses this special method at times and therefore when I opened my text document in notepad++ I didn't notice any weird characters. However, after opening it in notepad I discovered my problem and everything works beautifully now.

    Damn that was confusing for a while.

    If anyone can explain what the hell is up with Notepad++ I would find that very interesting.
    Last edited by Saurdo; February 19th, 2007 at 05:19 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012