Search File
Moderator: General Moderators
Search File
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?
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?
haven't tested this.
PS You should use a Database.
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
(and free... we're cheap)
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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"?
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"?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA