Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-18-2005, 04:10 PM   #1 (permalink)
phill2000star
Registered User
 
Join Date: Jul 2005
Posts: 4
phill2000star is on a distinguished road
Looping search strings.

Hi!

I'm trying to do something but im not too sure if it can be done (easily). What I am trying to do is take an array filled with values, and taking some form inputs from a HTML form, and searching the array to see which entries contain all the words searched for. So far I can get it to search the array for single value strings, which is fine, but when i add multiple words in the string, i seem to become stuck.

Here is what I am using so far.

PHP Code:
$query_array=explode(" "$query_string);  // split the query into single terms
if ($query_array[0] !="")                  // check 1st term isnt empty.
    
{
    if (
preg_match ("/$query_array[0]/i"$database_record))
        {
        echo 
$strRecord;
        }
    else
        {
        echo 
"A match was not found.";
        }
    }

All good so far. Now what i want it to do is to search for each term submitted. I want to do this using multiple preg_match's but dont know how to do it. For example if the user submits 5 terms to search for I would need something like:-

PHP Code:
if (preg_match ("/$query_array[0]/i"$database_record) && preg_match ("/$query_array[1]/i"$database_record) && preg_match ("/$query_array[2]/i"$database_record) && preg_match ("/$query_array[3]/i"$database_record) && preg_match ("/$query_array[4]/i"$database_record) ) 
etc....

Now this would work if i knew the user was searching for a fixed number of terms, but we all know that wont happen!! so will i need to use multiple if statements such as...

PHP Code:
$i=0;
if (
preg_match ("/$query_array[$i]/i"$database_record))
    {
    
$i++;
    if (
preg_match ("/$query_array[$i]/i"$database_record))
        {
        
$i++;
        if (
preg_match ("/$query_array[$i]/i"$database_record))
// more and more will need to go here for each term searched for, but how
// do I get it to loop for each term?? If I have to account for each possibility
// then will i need to hard write these steps over and over 100 times??
            
{
            echo
"match found";
            }
        }
    }
else
    {
    echo
"no match";
    } 
I am hoping that I am being clear in all this, and hope some die hard coders can help because this is really screwing with my head!!!

Many thanx guys!
phill2000star is offline   Reply With Quote
Old 07-18-2005, 04:31 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Why don't you start with an array of all possible candidates, pass that to function that accepts and regular expression to search for and returns an array of all the elements that match the regex that describes the first term. you can then pass that array to the next term and one you've gone through all your terms the final array returned is either empty or contains the elements that match all terms. The top loop iterates over the list of search terms so in psuedo code it looks like this.
Code:
$results = array(); //start it as an array containing everything you want to search
foreach( $searchterms as $term ){
   $results = reduce( $results, $term);
}
display( $results );
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 07-18-2005, 04:37 PM   #3 (permalink)
phill2000star
Registered User
 
Join Date: Jul 2005
Posts: 4
phill2000star is on a distinguished road
Nope....sorry ....lost me..... lol

I cant start the array with all the possible candidates.... the string entered by the user is completely random.

I didnt quit understand what your suggestion was though....any chance of simplifying it? I've been learning PHP for 2 weeks now, and although I'm getting there I still have problems!

Thanx again.
phill2000star is offline   Reply With Quote
Old 07-18-2005, 05:00 PM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Okay so you're making a form that where the user inputs search terms and those search terms are applied to the entries in a database right?
So the initial Array (we'll call it $results_array) would be filled with all the entries in the database. Since you can't hand code a hundred search queries you need to seperate out the searching from the normal flow. You have the users search terms in an array called $query_array.
You want to loop over query_array with a function that compares each value of results_array to a term from query_array and returns an array containing the matches.
PHP Code:
function reduce$results_array$search_term ){
 
$new_results = array();
 foreach( 
$results_array as $item ) {
  if (
preg_match ("/$search_term/i"$item)) {
   
$new_term[] = $item;
  }
 }
 return 
$new_results;

Now since that array will get smaller each time you run reduce with different terms, at the end of query_array, results_array will only contain entries that match all of the search terms.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 07-18-2005, 05:37 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
i had a couple questions that i seemed to delete, but i wrote some code anyway. i'm still kinda guessing at where the $database_record comes from and what it may contain.

also, if 'term' is just a word.

i may be a little off on what you are trying to do, but here is a quick script i wrote on assumptions. this code just searches a single string of text and displays how many matches are there.
PHP Code:
<?
// just setting up a string to search through
$text "This is an article about PHP Programming.  It has a lot of text in it.";

// here is what the user may be searching for from a post variable
$search trim($_POST['search_text_field']);

// now explode it into a words array
$words explode(" ",$search);

// setup a variable to count the matches
$words_matched 0;

// figure out how many words we have
$word_count count($words);

// if there are words to search through, let's begin
if( $word_count ){
    
    
// let's make the string we are searching lower case
    // so the searcher doesn't have to worry about case sensitivity
    
$text strtolower($text);
    
    
// now loop through your words array
    
foreach( $words as $word ){
        
        
// preg replace is a little strong if you are just trying to 
        // see if a word exists in a string, so let's use strstr()
        
if( strstr$textstrtolower($word) ) ){
            
            
// increment words matched
            
$words_matched++;
        }
    }
}


// now let's calculate and display the results
if( $words_matched == $word_count ){
    echo 
"All Words Matched!";

} elseif( 
$words_matched ){
    echo 
"Only ".$words_matched." words were matched.";

} else{
    echo 
"No matches found.";
}
?>
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search function with its own database Sonny1973 HTML, XML, Javascript, AJAX 3 03-28-2005 04:52 PM
SUID/GUID files search ! jackjill Linux / BSD / OS X 1 02-01-2005 06:25 AM
Search keystoneman Standard C, C++ 8 10-06-2004 08:04 PM
Search -That Can Match Out Of Order Items JBurke All Other Coding Languages 11 09-25-2003 03:09 PM
Please Review ZapMeta - New Search Engine khn Program Design and Methods 3 07-06-2003 10:15 AM


All times are GMT -8. The time now is 08:26 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting