PHP code (Search site without a database?)
Moderator: General Moderators
PHP code (Search site without a database?)
I want to add Advanced search in PHP without using MSSQL for my website. please help me...
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You want the PHP Filesystem functions. I will warn you now, this will be a slow way of doing things. You are literally going to have to open every file in every folder that you have, read each file into an array (or something like that), then match each line of each file against the user supplied search criteria term by term.
I wouldn't bother searching the files themselves .. that would be incredibly slow, and very difficult to actually code.
Have you considered using Google? http://www.google.com/searchcode.html allows you to embed a search box in your website that uses Google's index of the pages on your domain to search for things.
Have you considered using Google? http://www.google.com/searchcode.html allows you to embed a search box in your website that uses Google's index of the pages on your domain to search for things.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Nowhere near as straightforward as using a database but if you have linux then rock on cos you could use my all time favourite linux tool - GREP!sabarna wrote:Thanks. Yes, I want a user to be able to search flat content that is in the files on my server. I am not using any database.
Code: Select all
function search_files($string)
{
$ret = array();
$string = "'".str_replace("'", "\\'", $string)."'";
$results = `grep -ri --include=*.php $string .`;
$lines = explode("\n", $results);
foreach ($lines as $l)
{
preg_match('@(.*?):\s+(.*)@', $l, $matches);
$ret[] = array('file' => $matches[1], 'extract' => $matches[2]);
}
return $ret;
}Code: Select all
$results = search_files('sunshine');
print_r($results);
/*
Array (
0 => Array (
file => ./start.php,
extract => but we ended up spending 3 weeks in the sunshine before coming home
)
)
*/- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia