PHP search system without MySQL

4

I have a website with several HTML files, and I want to do a search engine in PHP, search for words in those HTML files, and show me the page or pages where they find themselves without using MySQL.

How can I do this?

    
asked by anonymous 30.03.2015 / 22:47

2 answers

2

"In PHP" is the biggest problem here, in my view.

If your site uses only HTML files, it may even be fairly straightforward, but no matter how you do it, you're likely to have problems with HTML tags being indexed for search, not counting search speed and other issues. Search engine is a very complicated business and has many "nuances" to consider.

Some issues relevant to your specific case:

  • Does this need to run on Linux or Windows?
  • Are these files 100% static or have PHP-generated content?
  • If they are static, how common is it to change these files?
  • My suggestion is very simple: Make a search box that uses Google, using "site:minha.url.com ".$search_params to get the best results faster.

    If you need to do this search from scratch or have other requirements, better develop the goal you want to achieve and why, so we can delve deeper into the subject.

        
    30.03.2015 / 23:43
    2

    Essentially, what you want is to use PHP to find a string within files.

    Two options, each one more suitable for the PHP version you are using:

    PHP > 5.0

    Using the DirectoryIterator class , we can browse a folder full of files and one by one find the desired string:

    // Palavra a localizar
    $string = 'bubu';
    
    // Iterar caminho para a pasta que contém os ficheiros
    $dir = new DirectoryIterator('minhaPastaCheiaDeFicheirosHTML');
    
    // Por cada ficheiro
    foreach ($dir as $file) {
    
        // Ler conteúdo do ficheiro para variável
        $content = file_get_contents($file->getPathname());
    
        // Ver se encontramos a string para fazer algo
        if (strpos($content, $string) !== false) {
    
            // Ena, encontrei, e agora?
            echo $file->getPathname();
        }
    }
    

    PHP > 4.3.0

    For older versions, we can make use of the glob() function, a bit slower than the solution above, but perfectly effective:

    // Palavra a localizar
    $string = 'bubu';
    
    // Caminho para a pasta que contém os ficheiros
    $dir = 'minhaPastaCheiaDeFicheirosHTML';
    
    // Por cada ficheiro localizado
    foreach (glob($dir."/*") as $file) {
    
        // Ler conteúdo do ficheiro para variável
        $content = file_get_contents($dir."/".$file);
    
        // Ver se encontramos a string para fazer algo
        if (strpos($content, $string) !== false) {
    
            // Ena, encontrei, e agora?
            echo $dir."/".$file;
        }
    }
    
        
    31.03.2015 / 00:00