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?
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?
"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:
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.
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:
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();
}
}
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;
}
}