If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > Get bold keywords in the search results

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-23-10, 08:38
supadema supadema is offline
Registered User
 
Join Date: Jul 2010
Posts: 18
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...
Reply With Quote
  #2 (permalink)  
Old 08-23-10, 10:14
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
you could use eregi_replace or strrepalce to repalce your keywords with the same keyword encapusalted with mykeyword
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 08-23-10, 10:56
supadema supadema is offline
Registered User
 
Join Date: Jul 2010
Posts: 18
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'.....
Reply With Quote
  #4 (permalink)  
Old 08-23-10, 11:42
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
?
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 08-23-10, 13:07
PriorityParts PriorityParts is offline
Registered User
 
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.
Reply With Quote
  #6 (permalink)  
Old 08-24-10, 04:12
supadema supadema is offline
Registered User
 
Join Date: Jul 2010
Posts: 18
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?
Reply With Quote
  #7 (permalink)  
Old 08-24-10, 04:32
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #8 (permalink)  
Old 08-24-10, 04:34
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #9 (permalink)  
Old 08-24-10, 05:23
supadema supadema is offline
Registered User
 
Join Date: Jul 2010
Posts: 18
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);
Reply With Quote
  #10 (permalink)  
Old 08-24-10, 06:55
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #11 (permalink)  
Old 08-24-10, 07:20
supadema supadema is offline
Registered User
 
Join Date: Jul 2010
Posts: 18
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...
Reply With Quote
  #12 (permalink)  
Old 08-24-10, 11:06
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #13 (permalink)  
Old 08-24-10, 11:11
supadema supadema is offline
Registered User
 
Join Date: Jul 2010
Posts: 18
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?
Reply With Quote
  #14 (permalink)  
Old 08-24-10, 11:29
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #15 (permalink)  
Old 08-24-10, 11:53
supadema supadema is offline
Registered User
 
Join Date: Jul 2010
Posts: 18
yes but then how do I make sure that no one can specify span, class,< ,= , ', bold, > or "?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On