Search File

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
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Search File

Post 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?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Is there a a reason why you are not using a database?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

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

Post 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.
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Now seems like a good time. ;)
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Post by djwk »

Does anyone know of a good tutorial site to start with?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

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

Post by RobertGonzalez »

I think a Google search for PHP and MySQL tutorials will yield a great grand result of information.
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Post 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" :)
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Post 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"?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The php manual has examples.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You could search LIKE field = '%search_term%' or you could do full text searching using MATCH AGAINST syntax.
Post Reply