codyodell
11-16-2006, 01:06 AM
I'm working on http://www.americanquotations.org/search.php .
What I'm trying to do is only return sentences from a result that contain the search term, so this is the procedure I'm following:
split returned quotes into sentences >> check if sentence contains search terms >> if so, return the sentence.
This seems fairly easy to do, but I've ran into a few problems, one being that if a user enters more than one search term duplicate sentences are returned. Another problem is that the $intSentences variable is way off it's intended value. here is the code I'm using:
while($row=mysql_fetch_array($result)){
// break into paragraphs
$strQuote = str_replace("\n", "<br><br>", $row['Quote']);
foreach($strSearch as $valuereplaced){
// highlight search terms
if($_GET['wholewords']){
$strQuote = str_ireplace(' '.$valuereplaced.' ', ' <span class="highlight">'.$valuereplaced.'</span> ', $strQuote);
}else{
$strQuote = str_ireplace($valuereplaced, '<span class="highlight">'.$valuereplaced.'</span>', $strQuote);
}
}
//split paragraphs into sentences
$splitQuote = split('[\.|\?|\!]', $strQuote);
foreach($splitQuote as $valuequote){
foreach($strSearch as $valuesearch){
if (strpos($valuequote, $valuesearch)){
$par .= '<p>'.$valuequote.'...</p>';
}
}
}
//number of sentences containing search
$intSentences = sizeof($splitQuote);
/*
PRINT RESULTS
*/
unset($par);
}
Can someone point me in the right direction to solving this? thanks
What I'm trying to do is only return sentences from a result that contain the search term, so this is the procedure I'm following:
split returned quotes into sentences >> check if sentence contains search terms >> if so, return the sentence.
This seems fairly easy to do, but I've ran into a few problems, one being that if a user enters more than one search term duplicate sentences are returned. Another problem is that the $intSentences variable is way off it's intended value. here is the code I'm using:
while($row=mysql_fetch_array($result)){
// break into paragraphs
$strQuote = str_replace("\n", "<br><br>", $row['Quote']);
foreach($strSearch as $valuereplaced){
// highlight search terms
if($_GET['wholewords']){
$strQuote = str_ireplace(' '.$valuereplaced.' ', ' <span class="highlight">'.$valuereplaced.'</span> ', $strQuote);
}else{
$strQuote = str_ireplace($valuereplaced, '<span class="highlight">'.$valuereplaced.'</span>', $strQuote);
}
}
//split paragraphs into sentences
$splitQuote = split('[\.|\?|\!]', $strQuote);
foreach($splitQuote as $valuequote){
foreach($strSearch as $valuesearch){
if (strpos($valuequote, $valuesearch)){
$par .= '<p>'.$valuequote.'...</p>';
}
}
}
//number of sentences containing search
$intSentences = sizeof($splitQuote);
/*
PRINT RESULTS
*/
unset($par);
}
Can someone point me in the right direction to solving this? thanks