View Single Post
Old 07-18-2005, 04:37 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
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