/    Sign up×
Community /Pin to ProfileBookmark

Question Regarding Search

I have a table which has the following fields “backupID”, “title”, and “content”. I have made a search page where it searches in all three fields looking for the word being searched.

I have come to a few problems:

1) I have made it so if it finds the word it makes that specific word in bold and yellow. but the thing is if I search for the word “james” for example, and there is a “James” it will find it but it will not make it bold. because of ascii problems Im guessing so what can I do?

2) If I search for (example) “villa moda” it will find villa moda but if I search for “moda villa”, it will not find anything with villa moda. I did fix this in a really stupid way because i needed to get it running but can anyone tell me how to do it the right way?

what I have done:

[code=php]
SELECT * FROM backup WHERE name LIKE ‘%$word[0]%’ OR name LIKE ‘%$word[1]%’ OR name LIKE ‘%$word[2]%’ OR name LIKE ‘%$word[3]%’ OR content LIKE ‘%$word[0]%’ OR content LIKE ‘%$word[1]%’ OR content LIKE ‘%$word[2]%’ OR content LIKE ‘%$word[3]%’ OR backupID LIKE ‘%$word[0]%’ OR backupID LIKE ‘%$word[1]%’ OR backupID LIKE ‘%$word[2]%’ OR backupID LIKE ‘%$word[3]%’ ORDER BY backupID DESC;
[/code]

as you can see I have used explode to put each word in an array (also explode only occures when there is a space which is a problem cause comma and other things dont work) and I only check the first 4 words (cause I was pretty sure noone would go above that) but this is not the best solution, … it could possibly be the wrost!

3) Now I want to be able to use quotes in my search so for example if someone writes “big brother” it will only find matches with “big brother” and not maches with the word big alone or the word brother alone.

Thanks in advance.

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@bokehSep 10.2006 — The highlighting is a simple matter but to sort out the rest you really should dump that query and switch to fulltext searches.
Copy linkTweet thisAlerts:
@robertketterSep 10.2006 — One really simple way to build db and search forms is my favorite website www.flattext.com Build the db you want, test it then send me the script ID and I will see what I can do to help you modify script to output the results the way you want them formatted. ?
Copy linkTweet thisAlerts:
@XeroSiSauthorSep 11.2006 — can anyone atleast tell me how I can make the highlight part work on upper or lower case! cause as I said when i search "billy" it finds all the "Billy", "biLLy" ... but it only highlights the ones that are exactly in lower case! ... what can I do here?!

Thanks Robert I will check it out!
Copy linkTweet thisAlerts:
@bokehSep 11.2006 — [code=php]if(!empty($_GET['highlight'])) # highlight search text
{
if(preg_match_all('@[^W]{3,}@', $_GET['highlight'], $matches))
{
$content = preg_replace(
'~b('.implode('|', $matches[0]).')b(?![^<]*[>])~is',
'<span class="highlight">$0</span>',
$content
);
}

}[/code]
Copy linkTweet thisAlerts:
@XeroSiSauthorSep 21.2006 — thanks this is working fine but I want I dont want the b part, I mean I for example if someone searches the word "shame" it will find "shameless" or things like that also! I dont want the exact word.
Copy linkTweet thisAlerts:
@NogDogSep 21.2006 — thanks this is working fine but I want I dont want the b part,....[/QUOTE]
Perhaps you could try taking the b parts out? (I mean, what's the worst that can happen? It's not going to blow up your computer into a cloud of smoke and flying plastic parts. ? )
Copy linkTweet thisAlerts:
@XeroSiSauthorSep 21.2006 — yeah I did but it wont show anything after that! sorry 2 be a pain! I have never worked with preg before.
Copy linkTweet thisAlerts:
@XeroSiSauthorSep 25.2006 — is there any source I can get to give me a standard search engine?! which I can read and know how to actually write a correct search.
Copy linkTweet thisAlerts:
@bokehSep 25.2006 — thanks this is working fine but I want I dont want the b part, I mean I for example if someone searches the word "shame" it will find "shameless" or things like that also! I dont want the exact word.[/QUOTE]That's what I hate about LIKE searches, they find words they shouldn't. And you actually want to highlight those words? Try this:[code=php]'~bw*('.implode('|', $matches[0]).')w*b(?![^<]*[>])~is', [/code]a standard search engine?[/QUOTE]Is there such a thing as [I]a standard search engine[/I]?
Copy linkTweet thisAlerts:
@XeroSiSauthorSep 28.2006 — what I mean is, I have never seen how a proper search engine is written. Whatever I am doing is based on the things Ive learned in university (which is close to nothing).

I accept what you say about having to highlight wierd answers, but this is a request from the client not my decision.
Copy linkTweet thisAlerts:
@bokehSep 28.2006 — I have never seen how a proper search engine is written.[/QUOTE]Is the page content all stored in MySQL?
Copy linkTweet thisAlerts:
@bokehSep 28.2006 — Here's one I wrote a little while ago. [B][URL=http://www.revista-foto.es/buscar.php?terminos=comprar+tests&x=0&y=0]Example![/URL] [/B]It returns the results as a list but doesn't include pagination as there were only about 30 pages total on the site.[code=php]function search_engine($search_text)
{
if(!preg_match_all('@[^W]{3,}@', $search_text, $matches))
{
return false;
}
connect(); // Database connect here
$search_text = clean($search_text); // strip slashes and escape
$now = time();

$query = <<<QUERY
SELECT page, title, content, embargo,
MATCH(title, content)

AGAINST ('$search_text' IN BOOLEAN MODE)
AS score
FROM pages
WHERE embargo < $now AND
MATCH(title, content)

AGAINST ('$search_text' IN BOOLEAN MODE)
ORDER BY score DESC
QUERY;

$result = mysql_query($query) or die(mysql_error());
$details = null;
if(mysql_num_rows($result) > 0)
{
$details .= '<ul>'."n";
while ($row = mysql_fetch_assoc($result))
{
$details .= '<li>'."n".
'<h3>'.
'<a href="./'.$row['page'].'.php?highlight='.implode('+', $matches[0]).'&amp;fecha='.($row['embargo'] + 1).'">'.
abbreviate_and_bold($row['title'], $matches[0], 30).
' - <span>'.date('j/n/Y', $row['embargo']).'</span></a></h3>'."n".
'<p>'.abbreviate_and_bold($row['content'], $matches[0], 160).'</p>'."n".
'</li>'."n";
}
$details .= '</ul>'."n";
}
return $details;
}

function abbreviate_and_bold($content, $words_array, $length)
{
$content = strip_tags($content);
if(($strlen = strlen($content)) > $length)
{
$backtrack = 60;
if((($start_position = strpos(strtolower($content), strtolower($words_array[0]))) !== false)
and ($start_position > $backtrack)
and ((($start_position - $backtrack) + $length) < $strlen))
{
$start_position = $start_position - $backtrack;
}
else
{
$start_position = 0;
}
$content = substr($content, $start_position, $length + 1);
$content = preg_replace(array('/^[^ ]*[ ]/', '/[ ][^ ]*$/'), array('', ' ...'), $content);
}
return preg_replace(
'~b('.implode('|', $words_array).')b(?![^<]*[>])~is',
'<span class="bold">$0</span>',
$content
);
}[/code]
Copy linkTweet thisAlerts:
@XeroSiSauthorOct 02.2006 — oh cool! thanks this helps alot!
Copy linkTweet thisAlerts:
@thunder77Oct 02.2006 — hey bokeh, would a variation of your search function work for my other thread that pcthug has been helping with? for some reason there's no errors but nothing is being returned when I search...

Thanks for the help!
Copy linkTweet thisAlerts:
@sitehatcheryMar 27.2007 — Hey, I know it's been a while, but this is an interesting topic. The issue that I've run into is that it also breaks apart hyperlinks by inserting spans on words that match.

Any thoughts on ways around this?
Copy linkTweet thisAlerts:
@bokehMar 27.2007 — Can you provide an example of something that is being broken?
Copy linkTweet thisAlerts:
@sitehatcheryMar 27.2007 — I found javascript from Fendall that seemed to do the trick. I had a link which read "http://www.somesite.com/Contact/contact.php". Since the link had 2 words that matched "contact", it only ignored one of them in the link and inserted a span in the other, breaking the link. It's working now, however.
×

Success!

Help @XeroSiS spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...