PHP code (Search site without a database?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sabarna
Forum Newbie
Posts: 3
Joined: Fri May 26, 2006 3:09 am

PHP code (Search site without a database?)

Post by sabarna »

I want to add Advanced search in PHP without using MSSQL for my website. please help me...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Your post is insanely vague. Can you elaborate a little bit more? Make sure to mention specifically what you are looking to accomplish and what you need help with.
sabarna
Forum Newbie
Posts: 3
Joined: Fri May 26, 2006 3:09 am

Post by sabarna »

Thanks for the reply. I want to have a Advanced search page for my website which will search whatever the viewer want to search from my site. I have created HTML form for that page. But don't know what to do next. Is there any process which doesnot include MSSQL? Please help me.....
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You want a user to be able to search flat content that is in the files on your server? Or are you using a database other than MSSQL?
sabarna
Forum Newbie
Posts: 3
Joined: Fri May 26, 2006 3:09 am

Post by sabarna »

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

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.
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!

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;
}
Example:

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
    )
)
 */
It's not tested but that the idea I'd go with. A pure PHP approacj would invlove some tricky recursion and lots of file_get_contents() followed by strpos() calls.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I've editted your thread title because "PHP Code" doesn't really tell us anything about what your question is ;)
Post Reply