Page 1 of 1

Search File

Posted: Mon Jun 04, 2007 9:10 am
by djwk
Hi,

I have 3 files with the following information:

1.dat
Hello

2.dat
Goodbye

3.dat
Bonjour


I want to search all 3 files for any part of the phrase in each file.


For example:

If I search "good" it will return the contents of 2.dat
If I search "jour" it will return the contents of 3.dat
And if I search the single letter "o" it will return the contents of all 3 files.

Could someone please point me in the right direction or give me some example code to play with?

Posted: Mon Jun 04, 2007 10:20 am
by hawleyjr
Is there a a reason why you are not using a database?

Posted: Mon Jun 04, 2007 10:38 am
by panic!
haven't tested this.

Code: Select all

<?

$search_for="cheeky";
$dir_to_search='files/';


if ($handle = opendir($dir_to_search)) {


  while (false !== ($file = readdir($handle))) {
       
		if(substr($file,0,1)!="."){ // no .. or . or hidden files in *nix systems
			
			$file_content=file_get_contents($dir_to_search."/".$file);
			
			if(stristr($file_content,$search_for)){
			          print $file." does contain the string '".$search_for."'<br/>";
			}else{
				print $file." does <strong>not</strong> contain the string '".$search_for."'<br/>";
			
			}

		}
  }

}


?>


PS You should use a Database.

Posted: Mon Jun 04, 2007 1:09 pm
by RobertGonzalez
What PHP version are you running? And try to database those types of things. The file system functions are more for file system functionality, of which what you are doing is not.

Posted: Mon Jun 04, 2007 4:18 pm
by djwk
hawleyjr wrote:Is there a a reason why you are not using a database?
I've never learn't how to. I've always gone flat file based. I should really learn how to use them.

Posted: Mon Jun 04, 2007 6:04 pm
by feyd
Now seems like a good time. ;)

Posted: Tue Jun 05, 2007 5:42 am
by djwk
Does anyone know of a good tutorial site to start with?

Posted: Tue Jun 05, 2007 8:32 am
by superdezign
W3Schools is always nice, but when you're completely oblivious.. It does absolutely nothing but throw keywords at you.

The best learning resource, for me, was the MySQL Manual that comes with the installation. Very detailed, and in MySQL, if you type "help" and then a keyword (ie. "help insert" or "help alter table"), you'll get the exact syntax for it's use.


MySQL and PHP are popular because they're well-documented. :D (and free... we're cheap)

Posted: Tue Jun 05, 2007 10:17 am
by RobertGonzalez
I think a Google search for PHP and MySQL tutorials will yield a great grand result of information.

Posted: Wed Jun 06, 2007 4:53 am
by djwk
Thanks for the info guys.

I'll get reading up on databases, I've always wanted to get into them, thanks for the "push" :)

Posted: Wed Jun 06, 2007 8:15 am
by djwk
Ok I've done some research and I now understand how databases work and how to use them however the tutorial I used didn't give an example of searching a database.

This code searches from the beggining of the database entry but I'd like to search ANYWHERE in the entry.

$result = mysql_query("SELECT * FROM example WHERE age like '2%' ")


Lets say I had a database entry "Hello"

How would I go about displaying the entire row if I searched "ell"?

Posted: Wed Jun 06, 2007 8:25 am
by feyd

Posted: Wed Jun 06, 2007 8:28 am
by djwk
Code examples anyone? I only started using databases a couple hours ago.

I can read through and understand code examples alot better than reading the php manual

Posted: Wed Jun 06, 2007 9:07 am
by feyd
The php manual has examples.

Posted: Wed Jun 06, 2007 10:17 am
by RobertGonzalez
You could search LIKE field = '%search_term%' or you could do full text searching using MATCH AGAINST syntax.