Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-14-2008, 09:15 AM   PM User | #1
sudhakararaog
Regular Coder

 
Join Date: Sep 2007
Posts: 137
Thanks: 1
Thanked 0 Times in 0 Posts
sudhakararaog is an unknown quantity at this point
string comparison

hi

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.

thanks.
sudhakararaog is offline   Reply With Quote
Old 07-14-2008, 09:28 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,638
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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 (
is_file($file))
    {
        
$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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 07-14-2008, 10:30 AM   PM User | #3
ess
Regular Coder

 
Join Date: Oct 2006
Location: United Kingdom
Posts: 865
Thanks: 7
Thanked 29 Times in 28 Posts
ess will become famous soon enough
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.
ess is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:18 PM.


Advertisement
Log in to turn off these ads.