4
I have a site with several HTML files, and I intend to make a search system in PHP, that looks for words in these HTML files, and that shows me the page or pages in which they are but without using Mysql.
How can I do it?
4
I have a site with several HTML files, and I intend to make a search system in PHP, that looks for words in these HTML files, and that shows me the page or pages in which they are but without using Mysql.
How can I do it?
2
"In PHP" is the biggest problem here, in my view.
If the site only uses HTML files, it may be fairly simple, but regardless of how you do it you will probably have problems with HTML tags being indexed for search, not counting the speed of the search and n other issues. Search system 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 make this search from scratch or have other needs, better develop the goal you want to achieve and why, so we can delve into the subject.
He made it clear in the question that they are html files, so they probably are .html
, in other words, static. Whether it’s Windows or like-Unix I honestly don’t see why we should take this into account if we’re usually using PHP Apis.
@Guilhermenascimento, he said he has several "HTML files", and I can use .html
in the extension, call html, and run PHP, Ruby, LISP or the devil in the file if I want to. The question was whether there was content dynamicity. About being Windows or Unix, it’s because one of the features of PHP is the functions exec()
, shell_exec()
and similar, which allow you to take advantage of system Apis, and which are a great option if you need to scroll through a reasonable number of files with performance closer to native, running from an interpreted language.
okay, it looks cool.
2
Essentially, what you want is to use PHP to locate a string within files.
Two options, each one more suitable for the PHP version you’re using:
Making use of class Directoryiterator, we can go through 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 function glob()
, a little slower than the above solution, 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;
}
}
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
I don’t have time to write an answer, but an initial idea would be to list all the files. html of a certain folder, and then go through each of them using
strpos
for his search.– Luis Henrique
with
file_get_contents
+stripos
(or preg_match) it is possible to do, however probably the performance will be "very bad", for this you must index the searches or the files and in this case you will need a database structure, no need to be sql, can be a file that works by lines, not put an answer because it is a little time to assemble such a system, but this is the way.– Guilherme Nascimento