Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Question need help - duplicate search results problem.

    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:

    PHP Code:
        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

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,837
    Thanks
    21
    Thanked 157 Times in 148 Posts
    I couldn;t quite follow the logic of your code (you have variables in there that haven;t been declared, or were declared in some previous code that is not shown), so why not give this code a try. It's untested, but maybe it will give you some ideas for how to do what you want to do:

    PHP Code:
    <?

    $search_term 
    urldecode($_GET['search_term']);
    $search_array explode(" ",$search_term);
    $sentence_array2 = array();

    /* Your MySQL query goes here */

    while ($row mysql_fetch_assoc($result))

        
    $quote $row['Quote'];
        
    $sentence_array $preg_split('/(\.\s*)|(\?\s*)|(!\s*)/',$quote);
        
        foreach (
    $search_array as $k => $v)
        {
            
    $v trim($v);
            
    $pattern '/'.preg_quote($v,'/').'/';
            
            foreach (
    $sentence_array as $key => $val)
            {
                
    $val trim($val);
                if (
    preg_match($pattern,$val) == 1)
                {
                    if (!
    in_array($val,$sentence_array2))
                    {
                        
    $sentence_array2[] = $val;
                    }
                }
            }
        }
        
        
    var_dump($sentence_array2);
    }

    ?>
    Last edited by chump2877; 11-18-2006 at 12:45 AM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts
    here is the full source if you were interested :

    http://www.americanquotations.org/backoffice/code.txt

    thanks for helping, I'll plug this code in ASAP and tell you how everything goes.


  •  

    Posting Permissions

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