Jump to content

I need help with this PHP search engine I have Modified


petree

Recommended Posts

Ok. About 6 months ago, I found this search engine on a website, and it works just like a regular search engine would. It searches through my website and sees if the keyword is in any of the files on my site. If a file has that keyword in it's body, the search engine displays that page in the search results. But when I got the code, all it showed in the results was where the file was located. For example, if I had a pages that had the keywords in it, it would display this in the search results:

 

./files/index.php ./hello/me/help.html ./files/file.html ./files/anotherfile.php ./files/store/home.htm etc...

 

It would display the results in one huge paragraph. It wouldn't separate the results with new lines, it wouldn't provide a link to the page, and it wouldn't have a description of the page. So, since I was unhappy with this, I decided to edit the script myself. So now, when you search for a keyword, it has all those things I wanted. The results even have descriptions, which is just a part of the body of that page.

 

But I have not yet addressed my problem. As I stated before, in my search engine, for each array, there is a link to the page, where the page is located, and a description (a section from the body of the page) of the page.

 

I have made a string called '$keyword' which is the word that the user searches for. Well actually, the keyword they insert into the search form is called '$keywordin' and the actual word is called '$keyword' but that is only so I can tell the difference between the two. And it also makes sure there isn't two descriptions showing up. But $keywordin=$keyword; so they are the same thing, so you don't need to worry about it.

 

The same thing applies to '$desc' and '$rdesc'. They are just different versions of the same thing. But they both have their own reason for being there. So just go along with me.

 

But anyway, in each description (which I have named $desc), I have told it that if it finds the inserted keyword anywhere in the description, to highlight and bold the word. That works fine. But the problem is that it's capitalization-sensitive. If a user inserted a keyword with a lowercase first letter, it would only highlight words with lowercase first letters. It wouldn't highlight the ones with uppercase first letters. If they inserted a keyword with an uppercase first letter, it would only highlight the words that had uppercase first letters. So, I need help with this script I made to make it NOT capitalization-sensitive. Here is the script I am trying to use to do this:

 

//if the first letter of the inserted keyword isnt capitalized, make sure to
//highlight any $keywords in the $desc that do have their first letter capitalized
//along with the ones that dont

if(ucwords($keywordin) != true){
$uppercase = ucwords($keyword);
$replacek = "$uppercase";
$replacementk = "<b><font style='background-color: yellow'>$uppercase</font></b>";
$stringk = "$rdesc";
$rdesc = ereg_replace($replacek, $replacementk, $stringk);
}

//if the first letter of the inserted keyword is capitalized, make sure to
//highlight any $keywords in the $desc that dont have their first letter capitalized
//along with the ones that do

if(ucwords($keywordin) == true) {
strtolower($keywordin);
$lowercase = strtolower($keyword);
$replacek = "$lowercase";
$replacementk = "<b><font style='background-color: yellow'>$lowercase</font></b>";
$stringk = "$desc";
$rdesc .= ereg_replace($replacek, $replacementk, $stringk);
}

 

The first part of the script DOES work. (the part that says: if(ucwords($keywordin) != true){ blah blah blah }). If they insert a keyword that has a lowercase first letter, then it will highlight the keywords that have capitalized first letters along with those that have lowercase first letters..

 

But the second part of the script DOESN'T work. If they insert a keyword with a capitalized first letter, it only highlights the keywords with capitalized first letters. It doesn't capitalize the keywords with lowercase first letters. But the second part of the script (if(ucwords($keywordin) == true) { blah blah blah }) is supposed to make sure that it highlights all of them.

 

Can someone please help me?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.