How to Create Your Own Search Engine for Your Website Using PHP 


               




How to Create Your Own Search Engine For Your Website Using





Enter Search Query:









In this article, we show how to build your own search engine for your website using just PHP.

This search engine we are going to build doesn't require MySQL in conjunction with PHP. It just requires PHP alone.

You can see the search engine shown above.

This search engine shown above searches this whole site.

This website has a pretty good, extensive list of articles on electronics. So if you type in 'transistor', 'resistor', 'capacitor', 'power', 'circuit', you'll get back a ton of results.

This search engine simply gives the URL of the pages that are found based on the search.

It lists every page on my website that matches all of the words in the search query.

If you try it out above, you'll see this.

Building this search engine literally took me about an hour or 2 to code.

It's very simple and it doesn't require any vast enormous databases such as using MySQL database and having multiple tables to search. Its' just PHP.

So how does this work?

So, as you probably know if you run a website, you upload files to your website through a file transfer protocol (FTP). These files can place in a directory on your website. This can be the home directory (/), the Articles directory (Articles/), the images directory (images/), or whatever else directory you created and named. You're probably familiar with this if you run a website. Files are uploaded to directories (or folders) on your website.

So you have files in either one directory or various directories.

Using PHP, you can access any directory on your website.

Being that PHP is a server side language, it can access back-end things such as a website's directories.

So once we gain to a directory on your website with PHP, we can search it. We create a search text box such as what you see above and then we extract the query entered and see if any files in that directory match the query. If it does, we output it in the form of a hyperlink.

And that's as simple as it is.

We either can search one directory or multiple directories on your website.

A user enters a search query.

If the search query matches anything in the files of the directory (or directories) being searched, we can output the hyperlink. As many pages that match the query get outputted.

So it's very simple.

The only thing is that your files must have appropriate names. For example, if you have a file on dandelion flowers. It will have that in the file name, for example, how-to-feed-dandelion-flowers.html or how-to-grow-dandelion-flowers.php or just dandelion-flowers.html. It has to have the name in the filename. If you have an article on dandelion flowers and it's named 123wxp.html, this search engine won't work. It's based on the fact that it looks at the file name. The file name should have the keywords that the user is searching for.

So now that you get an idea of how it works, we're now going over all the code needed to get this up and running on your website.

If you just want the code, the full code of this image search engine is at the following link: Code to create your own search engine for your website using PHP. Remember that you have to change the directory to the directory you want to search. Also you have to change the website name to your website name when outputting the link to the search engine result returned. Also when you copy this code to your web page, make sure it's saved as a PHP file.

HTML Code

So now we'll first go over the HTML code needed for this search engine.

The HTML code is needed just to create the search text box.

Everything else is PHP.



So the above code creates the search box.

First, we create an HTML form. We set the action equal to "" because we want the information to this page. If we wanted the information sent to another page, we want specify this file within this action attribute. We set the method equal to POST.

As almost all forms require, we add a submit and give it the value, "Search". Thus, Search is displayed on the button.

We also add PHP code so that if the submit button is clicked and the search text box is empty, the statement "A search query must be entered" is output.

We then close the form.

This concludes the HTML code needed to make this search engine.


PHP Code


First Block of PHP Code

We now go on to the PHP code necessary to make this search engine work.

We separate the PHP code into 2 blocks of code.

The first block of PHP code is shown below.





This PHP extracts the search query that the user enters into the search text box.

We then a $searchoriginal variable so that the search term that the user has entered is kept intact. We later will be doing many modifications to the search query that the user has entered.

We then make all the characters of the search query that the user has entered lowercase. This is because case matters in PHP. By making all the characters lowercase, we don't have to worry about case sensitivity.

We then use the PHP $trim function to cut off any space in front of the search query or the back of the search query (or left of the query or right of the query).

Next, we explode the $search variable. The explode() function in PHP separates a string based on a certain parameter. Here, we want the $search variable explosed based on any space characters (" "). The reason we want to do this is because the user may type in more than one word into the search engine. For example, the user may type in "transistor circuit". If we forget about exploding the term and we just accept the search query directly, only terms that have the direct phrase, "transistor circuit" will be returned. If a page is named, How-to-connect-a-transistor-to-a-circuit, this page will not be returned because the term "transistor circuit" isn't consecutive to the string. The terms in the name are separated. So we'll miss this search. Only a term that has "transistor circuit" with the terms exactly "transistor circuit" will be returned.If these 2 terms are separated in the name, this search will be skipped. To avoid this, we explode the term and then look at each term separately. If the name of the file has both terms, it will be returned in the search results. Another example of this is a user typing in, "pepperoni pizza". Without exploding the term, only results that have the phrase "pepperoni beef pizza" in that exact order will returned. A page that is named, "Adding pepperoni and beef to pizza" will not be returned because the phrase "pepperoni beef pizza" isn't in it, even though all the terms are in it. So you can see why we wouldn't want to just use the phrase exactly that the user inputs.

So after we explode the search query based on spaces, we then count the number of terms in the $search variable. Again, the explode() function separates the string into separate entities based on the parameter entered, in this case a space (" "). We now count the number terms in the $search variable. This is how we know how many terms the user has entered in.

Lastly, we make sure that we obtain the value of the submit button to see if it has been clicked. If it has been clicked, it returns true. If not, it returns false.

Second Block of PHP Code

This is now the second and last block of PHP code shown below.



So this, you can see, is the longer block of PHP code. Still, it isnt' hard.

First, we specify which directory we want to search that is on our website.

Practically, all of my website's pages are in the Articles directory. So I set the directory equal to "Articles/" Change this to the directory you want searched.

If you want to add multiple directories, copy and paste all of this code and change the directory information. That way, you can add all the directories you want searched on your website.

So once we've specified the directory, we then go to the heart of code. We first check to see if the submit button has been clicked. If it has, the code advances to the next line. We have a second if that that checks to see if a search query has been entered. If it has and the search query isn't empty (a null string), the code advances to the next line. The next line is a third if statement. The line checks to see if the directory is actually a directory. If it is, the code advances to the line. The line opens up that directory.

We then have another if statement. If the number of terms in the search query is just one single word, the while loop is executed. The while loop loops through the files in that directory.

Because we're going to modify the file name, we created a $fileoriginal variable that holds the file intact. This is important because we'll need when we're outputting the link to the file.

We then make the $file lowercase. By making the search query lowercase in previous and making the $file lowercase now, we have equal cases. Therefore, we don't have to worry about cases matching. All are lowercase, so they're at equal plane.

Since most files are separated by "-" or "_", we use the PHP str_replace() function to replace these symbols with spaces. This make the link more human readable than if it had all these symbols.

Next, we have to eliminate the ending of the filename, such .html, .php, etc. This is not needed when we want to view the results returned from the search. Again, this makes the results returned more human readable. We don't care whether the results returned are html or PHP pages.

So we go about doing this by finding the position of the ".". Once we know its position, we know that what's after the "." is the file extension (.html or .php, etc).

So we then use the PHP substr() function to just take the portion of the file that isn't the extension.

So now we just have the file name without the file extension.

Next, we have if an if statement. We use the strpos() function in PHP that if the search term is in the file name, that result is going to be returned. If the search query is in the file name, this if statement returns true. If so, it advances.

We then use the PHP ucwords() function to capitalize each word in the string. It again is done to make the results returned more human readable and better visually.

Now we create an array that stores if any files are returned that match that query. The reason we do this is because later on, if no results are returned from the search query entered, we then count this array. If it is equal to 0, no files have been returned that match the search query, which means we can output a statement, "No results found for this search query".

We then use the PHP echo () function to output the links to the pages found that match the search query. Whether it's 1 result or 50 results, all are returned that match the search query.

Next we have an if statement if the search query has 2 terms, meaning the user has entered 2 words into the search box. All the code is the same except for the last if statement in that block of code. Now since there are 2 search words, the if statement is composed of 2 strpos() functions. If the file name has both terms in it, then it is returned in the results.

This search engine covers the user entering up to 7 words in the search box. For search, that usually is sufficient. However, if you want to make it more just copy and paste more blocks of code and just change the value of $countsearchterms and up to $search[number] value. I would say the maximum would be 10. I don't think a user is going to type in 30 words into a search engine.

After that, we close the directory.

We then count the $array.

If the $array is equal to 0, then no search results have been returned. So we output the statement, "No results have been found for this search".

And this concludes all the code needed to create a search engine for your website.

Again, no database involvement. No MySQL. Just straight PHP code directly to directories on your website.


Related Resources

How to Create a Search Engine Using PHP

How to Create a Custom Search Engine For Your Website Using PHP

How to Create an Image Search Engine For Your Website Using PHP

How to Create a Video Search Engine For Your Website Using PHP

How to Create an Audio File Search Engine For Your Website Using PHP


HTML Comment Box is loading comments...