Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Making search through text file?

  1. #1
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Making search through text file?

    Hey guys, I got a form on an html that sends a string to a php script, and I want to search through a textfile to find any partial matches to the string that was inputted.

    So let's say the textfile was delimited by | or an endline between each term.

    Anyone know how to get me started? I only started php recently, but have been doing c++ for a year or so now. So please try to explain the syntax if possible as well, thanks.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    if you have each term on a new line, you can use file() to read the file into an array, and then use preg_match() to compare the submitted string to each term:
    PHP Code:
    <?php

    $terms 
    file('path/to/file.txt'FILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES);
    $input $_POST['user_submission'];

    foreach(
    $terms as $t){
       if(
    preg_match($t$input)){ echo 'Found match: '.$t.'<br>'; }
    }

    ?>

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Note: file_get_contents() is like file() but doesn't mess with splitting new lines into an array. That may or may not be helpful.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    $terms file('path/to/file.txt'FILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES); 
    What does FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES do? It looks to me like it is some sort of function or something. I'm really not used to PHP because it is such a high level language.

    Also, what if there's also a delimiter to some of the terms? Basically it is sort of like, a movie data base search or something. Where you have on one line in the format of

    Actor(s) | Director | Movie Name

    Where | is a delimiter. So if I search for, Daniel Craig for example, it'll short circut itself and go to the next line to see if that is in there.

    By the way, I really appreicate the help. Thanks.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    In the code posted above the delimiter is not handled. It will simply give you the whole row in which that is found.
    You can use explode('|',$variable) to split a text string into parts around a delimiter, but I'm not sure exactly what you want to do with that.

    As for the capitalized strings at the end of the function, they are parameters based on PHP default constants.
    To explain, a constant is like a variable except that it cannot change its value (ever).
    define(CONSTANT,'value');
    Those above are predefined by PHP to be certain values. The idea then is that they are parsed by the function (based technically on their assigned values), then this makes the function do different things such as in this case "ignore new lines". In this type of use in a function, they are called "flags" in the sense that they are markers for certain parameters to be processed.
    Now that's just the general explanation. I'm not sure about these specific items because I haven't had the need to use them.
    But for questions like this it's always a good idea to refer to the php.net reference page for the function in question. They'll be defined there:
    http://php.net/manual/en/function.file.php
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Alright, I think I can somehow do this. Thanks a lot guys!

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    The FILE_IGNORE_NEW_LINES flag removes the \n (newline) character from each line of the file, so you'd end up with 'this is a line from the file' instead of 'this is a line from the file\n'.

    The FILE_SKIP_EMPTH_LINES flag tells the function to skip lines with no content (otherwise, you'd end up with empty strings in your array whereever there was an empty line in the file).

    Edit:

    in your post above, it looks like you may have multiple "actor" names on each line? If so, you'd need to use a second (different) delimiter (e.g., "actor one,actor two|director|movie name" and use explode() twice. With this complexity, however, it would be much quicker (and simpler) to simply use a database.

    Good luck!

    Last edited by traq; 04-02-2010 at 07:56 PM.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I agree about using a database. Once the search operations become complex (such as having one thing embedded in another thing then you want to search), it's much faster and easier to use a database.
    In theory you can use a text file for anything, but the programming for databases will be much more efficient (and faster), and it will be more standard so it will be easier to figure out.

    Databases are a bit harder at the basic level but from there it's a lot easier to get to an advanced level.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I understand that the normal use is a database but, being that this is a assignment, prof tends to come with the theoratical non-practical real life situation.

  10. #10
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Homework assignments are not allowed to be discussed here and requests for such will quickly get locked. The reason is that getting your work done for you on this forum is sorta like cheating.

    I do find the topic somewhat interesting to read. I do not think the parameters of what is being requested have been clearly defined though. Storing the data in a database and then searching the database is the best way to go whenever possible. It is faster, more efficient, and you get more accurate results too with less margin for error.

    I think that it is important to be able to search through a text document, though, because sometimes you need to search through one or multiple php files for a particular line of code for the purpose of fixing a hard to find error or errors.
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

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