Can a search bar built in PHP can also search static web pages?

1.5k views Asked by At

I want my website to have a search bar, my site is created in core PHP.

Some section of my site has static pages and some section have dynamic pages.

  1. My developer says that it is not advisable to build a customised search bar in PHP that can work on the static pages, as it would be too slow, because it would do an in-folder search. As per him cutomised search bar would work best on only dynamic pages, and hence I should implement a Google API.

  2. Why I was not interested in building a Google API search box? Because my developer was not able to customize it design and the standard box was not blending with my site's design.

Any suggestion or advise on how to do either points.

4

There are 4 answers

0
Javlon Tulkinov On

If you save static pages in the Database, work be easier

0
Ivijan Stefan Stipić On

Like Jawlon Rodriguez says, you need to create table in database for static content, save all text and HTML, CSS, JS content in table and lather fetch that out. Is better and faster.

0
Marvin Fischer On
  1. It would be really slow he is right. But with the right caching system it is possible to catch that.

  2. Google searchbar is slightly customizable like this: http://www.elitepvpers.com/forum/ but i really gives you some great features and a slight google boost

1
thpl On

Why don't clients trust their developers? He's absolutely right.

  1. A dynamic search through static pages would indeed require a search through the File System which is slow as hell (could be prevented by caching though).

  2. A google searchbar is not really customizable and that's not because "he was not able to do it", but because it's just restricted by Google. You use their products? You get their branding. It's as simple as that.

  3. PHP gives you the power to create dynamic websites. You just need to use it. If you store your content in a database you would have the advantages of a fulltext search which is great.

  4. If you've got a dead simple website consider to set up a XML (or whatever format you prefer) with the static content and link it with the URL of the actual page. That could look like this for example:

XML File

<?xml version="1.0"?>
<pages>
    <page>
        <url>http://google.com</url>
        <title>Some title</title>
        <content>
            The quick brown fox jumps over the lazy dog
        </content>
    </page>

    <page>
        <url>http://yahoo.com</url>
        <title>Ohai Catz</title>
        <content>
            The quick Cat kitten cat cat
        </content>
    </page>
</pages>

Sample PHP

<?php

// String to search for
$searchString = 'Cat';

$xml = simplexml_load_file('staticpages.xml');

$pageMatches = [];

foreach( $xml->page AS $page )
{
    // Check if the keyword exists in either the title or the content 
    if( stristr($page->content, $searchString) !== false || stristr($page->title, $searchString) !== false )
    {
        $pageMatches[] = $page;
    }
}
?>

<?php echo $searchString ?> has been found on the following pages: <br>

<?php foreach( $pageMatches AS $match ): ?>
    <a href="<?php echo $match->url ?>"><?php echo $match->url ?></a><br>
<?php endforeach ?>

Output

Cat has been found on the following pages: 
http://yahoo.com

The advantages of that approach:

  • No database
  • With small tweaks you could let the contents of your site be displayed from the XML, so you had a central source of your data
  • You could add additional information like tags or keywords to the XML to make the search more precise

Now the big disadvantage that instantly disqualifies this method for efficient searching:

  • You need to implement a search algorithm.

I simulated it with stristr(). If you want a search with usable results you have to put a lot of work and effort into it. Maybe you'll stumble upon some algorithms on the internet. But search algorithms are a science in it's own. Google isn't a multi-billion dollar company for nothing. Keep that in mind