Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Thanked 0 Times in 0 Posts

    string comparison


    i am writing a small application where a user enters a phrase in the textfield and i would like to display all the files

    present in the root directory which consists of the keyword or keywords entered by the user.

    i have used a few comparison functions but i am not getting the expected result.

    $my_file = file_get_contents("filename.html");
    what ever the user enters whether it is a single word or few words i would like to compare with $my_file in a case

    insensitive manner.

    can anyone suggest the best method and how to go about.


  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,662 Times in 2,631 Posts
    Pattern matching is the best route to use. I don't know what format you'll allow, but I will say its a single text entry line separated with spaces. This example (though untested) will try to scan only a single directory and is not built for recursion.
    PHP Code:
    $keywords str_replace(' ''\'|\''$inputKeywords);
    $matches = array();
    foreach (
    glob('*') AS $file)
        if (
    $sFileContents file_get_contents($file);
            if (
    preg_match('/\'[' $keywords ']\'/msi'$sFileContents))
    $matches[] = $file;

    I don't know if something like this will work, but off hand it looks like it should do the job. $inputKeywords is whatever you're entry is, including whatever cleaning you need to do to it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  3. #3
    Regular Coder
    Join Date
    Oct 2006
    United Kingdom
    Thanked 30 Times in 29 Posts
    Check out strcmp and strcasecmp to perform both sensitive and insensitive string comparisons.

    Fou-Lu has shown how to perform this operation using regular expressions, which is handy in the event that you want to continue searching if a user miss typed keywords etc.


Posting Permissions

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