0

new php programmer here, I apologize if this has already been asked:

I have an array filled with strings. Each string is the exact name of a page on my site, so the array is a sort of 'database'.

I'm wondering how to create a search engine that will let the user enter a keyword. The engine will then crawl my array of strings for any matches, and return a list of pages on my site that include the keyword in the pagename.

Any help is appreciated, not sure which functions to use to build this.

Thanks in advance!

2
  • Wouldn't you rather search the page contents instead of the page titles? Commented May 30, 2012 at 17:14
  • Hi Jack, no I'm looking to search titles only. Commented May 30, 2012 at 17:57

3 Answers 3

0

I guess that you could just use strpos to see if the users term is contained inside of an array key. You would have to loop through each page, though, and see if the search term is contained within the pagename.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool! Thanks for the tip. I'll do some research on strpos.
0

The way I would approach this is by creating a binary search tree with all the strings. That would make the search faster and more efficient. Here is a LINK that could help you achieve that.

Hope this helps.

1 Comment

No problem! If it helps, please promote my answer! :)
0

I ended up solving this problem by using a flat file and conditionals, here is the code:

$userlink = $_POST['link_to_test'];
$linkdatabase = array('...');
if (in_array($userlink, $linkdatabase)) 
{
echo 'Data already exists';
}
else { ....

1 Comment

When providing code that solves the problem, it is best to also give at least a short explanation of how it works so that folks reading won't have to mentally parse it line by line to understand the differences.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.