Results 1 to 15 of 15
  1. #1
    Join Date
    Jul 2010
    Posts
    20

    Unanswered: Get bold keywords in the search results

    Hello everybody

    I'm doing a simple search engines for my website.
    I'm trying to find a right way for my case to get in bold the keywords in the search results; I've found a function that seems to be interesting, but I'm not able to integrate it in my code... is somebody able to help me?

    The function is at the end of the code below:

    PHP Code:
    if ($_POST['search']){
        
    # If the string is empty:
        
    if ($_POST['keywords'] == '') {
            print(
    "<div class='risultati'><b>Insert at least one keyword.</b></div><br>");
        }
        
    # If the string is not empty:
        
    else {
        
    $searchStr $_POST['keywords'];
                
    preg_match("/[^\w\._-]/i",$searchStr);
                
    # Connect to the database:
                
    require ("db/db.php");
                
                print 
    "<div class='risultati'>Results of the research for \"<b>$searchStr</b>\":</div><br>";
                
                
    # Separate the keywords entered with one space
                
    $keys explode(" "$searchStr);
                
                
    # Select from database where exist the keyword inserted
                
    $querystr "SELECT img,link,numclick,descrizione,ordine FROM totale_tabelle WHERE 1=1 ";

                for (
    $x 0$x count($keys); $x++) {
                   
    $querystr .= "and concat( descrizione, ordine ) like \"%$keys[$x]%\"";
                }
                
    $querystr .= " LIMIT 20";
                
    $result mysql_query($querystr);
                    
                    
    # If find keywords
                    
    if ($frow mysql_fetch_array($result)) {
                    
    $found true;
                    do { 
                        
                        
    # Make bold the words corresponding at the keywords
                        
    $string $frow["descrizione"];    
                        
    $array $keys;
                        
    boldText($string$array);
                        
                        
    # Print the records where have been found the keywords:
                        
    echo "<div class='grid_8'><div class='screen'>".$frow["img"]."</div><div class='text'><h2>".$frow["link"]."<span class='visite'> &nbsp;- ".$frow["numclick"]." visite</span></h2>".$string."</div></div>"."<br>";
                        } while (
    $frow mysql_fetch_array($result));
                    }
            
            
    # If has not found any keywords:
            
    if (!$found) {
                print(
    "<div class='risultati'><b>Nessun risultato correlato alla ricerca  stato trovato.</b></div><br>");
            }
        }
    }

    function 
    boldText($string$array){
    $string strip_tags($string);
    return 
    preg_replace('~('.implode('|'$array).'[a-zA-Z]{0,45})(?![^<]*[>])~is',
    '<strong>$0</strong>'$string );} 
    In this way I don't get any bold...
    Many thanks if somebody can lose a little bit of time to help me...

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    you could use eregi_replace or strrepalce to repalce your keywords with the same keyword encapusalted with mykeyword
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Jul 2010
    Posts
    20
    Ok, let's say that before I tried in this way

    $words = $frow["descrizione"];
    $boldwords = str_replace($searchStr,"<b>".$searchStr."</b>",$words);

    $searchStr is the keyword that the user write in the form

    The problem that I wasn't able to resolve like this is that I get in bold just the words that are exactly the same of the keyword string.

    For example if my keyword is 'dogs' everytime I've the word 'dogs' I get bold and ok, but if I write 'dogs and cats' I get bold only where I have 'dogs and cats' and not if have just 'dogs' or 'and' or 'cats'.....

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    ?
    OK so split your key words into individual words
    stuff those words into an array
    iterate through the array and encapsualte the words as before.
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Aug 2010
    Location
    NYC
    Posts
    10
    Healdem's technique is best. Use the explode() function. Also, I recommend using a CSS class instead of a straight up <b>. That way if you want to change the style across the board, its very easy.

  6. #6
    Join Date
    Jul 2010
    Posts
    20
    do you mean something like this:

    $words = $frow["descrizione"];

    $boldwords = str_replace($searchStr,"<span class='bold'>".$keys[0].$keys[1].$keys[2].$keys[3]."</span>",$words);

    is it correct?

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    is it correct, I don't know, why don't you try it and see?
    whats the phrase... "learn by doing"......

    can you explain what you think that code you have just put up does. think it through, then compare what it does with what you want it to do. if the two match.. jobsagoodun. if not diagnose why it isn't doing what you want then take steps to resolve that diagnosis
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Id expect somthing like

    for all keys in the array
    myoutput = repalce(thiskey with "<span class='bold'>".thiskey."</span>" in myoutput)
    next key
    I'd rather be riding on the Tiger 800 or the Norton

  9. #9
    Join Date
    Jul 2010
    Posts
    20
    ok, i think i'm doing progress
    like this i've divided the keywords, now the only thing i've not understood is how i print them... for example if i do

    echo $keyword_1 keyword_2 of course i get two times the same text...

    $keyword_1 = str_replace($keys[0],"<span class='bold'>".$keys[0]."</span>",$words);
    $keyword_2 = str_replace($keys[1],"<span class='bold'>".$keys[1]."</span>",$words);
    $keyword_3 = str_replace($keys[2],"<span class='bold'>".$keys[2]."</span>",$words);

  10. #10
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    PHP Code:
    $Words $frow["descrizione"];
    //lets assume your keywords are in an array called $KeyWords
    foreach ($KeyWords as $KeyWord)
    $Words str_replace($KeyWord,"<span class='bold'>".$KeyWord."</span>",$Words);

    so descrizione contains the text you want to highlight anmd is copied to $Words
    you have an array $KeyWords with each of the words you want to highlight
    the foreach statement iterates through every element in the array
    the $Words text is scanned for each occurance of a key word, where found it adds the 'span' tags, and then moves onto the next keyword untill the array is exhausted.
    having done that you have a variable $words which can be treated/handled as you wish
    I'd rather be riding on the Tiger 800 or the Norton

  11. #11
    Join Date
    Jul 2010
    Posts
    20
    ok, i've tried like this.
    i get now only the last keyword of the array in bold, if i write "hello world" i get in bold just world... but i think to have done exactly what you've said...

  12. #12
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so lets see what you've done.....
    BTW you need to make sure no one can specify span, class,< ,= , ', bold, > or " otherwise you will get carnage
    I'd rather be riding on the Tiger 800 or the Norton

  13. #13
    Join Date
    Jul 2010
    Posts
    20
    This is what i've done:
    PHP Code:
    $words $frow["descrizione"];
                        foreach (
    $keys as $key) {
                            
    $words strip_tags($words);
                            
    $words str_replace($key,"<span class='bold'>".$key."</span>",$words);
                        } 
    p.s. $keys is what you supposed with $KeyWords

    Is it necessary i post also the code around that?

  14. #14
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    what do you think 'strip_tags' does?

    was it in the suggested code

    do you think that may be the cause of your problems?

    when you examine the output from the code did you notice anything odd, anything that you didn't originally expect?
    I'd rather be riding on the Tiger 800 or the Norton

  15. #15
    Join Date
    Jul 2010
    Posts
    20
    yes but then how do I make sure that no one can specify span, class,< ,= , ', bold, > or "?

Posting Permissions

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