1

hello im a newbie in php i am trying make a search function using php but only inside the website without any database

basically if i want to search a string namely "Health" it would display the lines

<a href="foobar.html">The Joys of Health</a>
<a href="foobar2.html">Healthy Diets</a>

This snippet is the only thing i could find if properly coded would output the "lines" i want

$myPage = array("directory.php","pages.php");
$lines = file($myPage[n]);
echo $lines[n]; 

i havent tried it yet if it would work but before i do i want to ask if there is any better way to do this? if my files have too many lines wont it stress out the server?

3 Answers 3

1

The file() function will return an array. You should use file_get_contents() instead, as it returns a string.

Then, use regular expressions to find specific text within a link.

1

Your goal is fine but the method you're thinking about is not. the file() function read a file, line by line, and inserts it into an array. This assumes the HTML is well-structured in a human-readable fashion, which is not always the case. However, if you're the one providing the HTML and you make sure the structure is perfectly defined, ok... here you have the example you provided us with but complete (take into account it's the 'wrong' way of solving your problem, but if you want to follow that pattern, it's ok):

function pagesearch($pages, $string) {
    if (!empty($pages) && !empty($string)) {
        $tags = [];

        foreach ($pages as $page) {
            if ($lines = file($page)) {
                foreach ($lines as $line) {
                    if (!empty($line)) {
                        if (mb_strpos($line, $string)) {
                            $tags[$page][] = $line;
                        }
                    }
                }
            }
        }

        return $tags;
    }
}

This will return you an array with all the pages you referenced with all occurrences of the word you look for, separated by page. As I said, it's not the way you want to solve this, but it's a way.

Hope that helps

0

Because you do not want to use any database and because the term database is very broad and includes the file-system you want to do a search in some database without having a database.

That makes no sense. In your case one database at least is the file-system. If you can accept the fact that you want to search a database (here your html files) but you do not want to use a database to store anything related to the search (e.g. some index or cached results), then what you suggest is basically how it is working: A real-time, text-based, line-by-line file-search.

Sure it is very rudimentary but as your constraint is "no database", you have already found the only possible way. And yes it will stress your server when used because real-time search is expensive.

Otherwise normally Lucene/Solr is used for the job but that is a database and a server even.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.