Log in

View Full Version : How to search a word in text file.



pankaj.ghadge
11-17-2008, 05:23 AM
Hello ,

I want to search a word from a text file.
e.g search word:- hello, hi welcome to php
Search is separated by comma or by space.

Consider there is one directory that contain many files and i want to search that word in every file. if "hello hi welcome to php" words found in these file not continuously .The result will show the link to these file. when user will click on these file it will show the contain (Search word will be shown in different color like in yellow color).

File may contain these word (HELLO HI WELCOME to Php) in capital but still i want to show the result .

e.g

<input type=text name=txtSearch id=idSearch><input type=button value=SEARCH onclick='search(idSearch.value);'>

Result will show the links to these file ..

File1.log
File2.log
File3.log
File4.log

When user will click on these link then file contain will be show to user . These search word will be shown in different color (like in yellow color).

james438
11-17-2008, 09:00 PM
I know you said this already, but just to confirm, you want to search .txt files?

james438
11-18-2008, 04:11 AM
well.... here is a little something to get you started if this is the route you really want to go.


<?php
$handle = file_get_contents("test_text.txt",NULL);
$a="hello, hi welcome to php";
$a=str_replace(",","",$a);
$a=explode(" ",$a);
$c=0;
foreach($a as $y){
if (stristr($handle,"$a[$c]")) $b[]= 'yes';
else $b[]='no';
$c++;
}
echo $handle;
if (in_array("no",$b)) echo '<br><br>did not match';
else echo '<br><br>we have a match';
?>

It will scan one document for the selected terms. If they all appear then "we have a match". If one of the terms is not found in the document then "did not match".

You should be able to figure the rest out from here with the possible exception of gathering all of the file names in a directory. Either way this should help you get started. Let us know if you get stuck anywhere.

EDIT: I kinda regret posting the above code. It does have its uses for correcting poor coding across directories of bad files, but to use it as a search program is really a poor way to use it. data like this should be stored in a database like MySQL and searched that way. You will have far greater control over your searches and the coding will be much simpler as well.

kaytan
02-06-2009, 06:04 PM
well.... here is a little something to get you started if this is the route you really want to go.


<?php
$handle = file_get_contents("test_text.txt",NULL);
$a="hello, hi welcome to php";
$a=str_replace(",","",$a);
$a=explode(" ",$a);
$c=0;
foreach($a as $y){
if (stristr($handle,"$a[$c]")) $b[]= 'yes';
else $b[]='no';
$c++;
}
echo $handle;
if (in_array("no",$b)) echo '<br><br>did not match';
else echo '<br><br>we have a match';
?>

It will scan one document for the selected terms. If they all appear then "we have a match". If one of the terms is not found in the document then "did not match".

You should be able to figure the rest out from here with the possible exception of gathering all of the file names in a directory. Either way this should help you get started. Let us know if you get stuck anywhere.

EDIT: I kinda regret posting the above code. It does have its uses for correcting poor coding across directories of bad files, but to use it as a search program is really a poor way to use it. data like this should be stored in a database like MySQL and searched that way. You will have far greater control over your searches and the coding will be much simpler as well.
hie ,
a little question, why this script doesn't work?
<?php
$f='test.txt';
$t='kaveh';
$tp=fopen($f,'r');
$fd=file($f);
for($i=0;$i<=count($fd);$i++){
$temp=$fd[$i];
if($temp==$t){
echo 'found on:'.$i;
}
}
fclose($tp);
?>

wattoo
03-16-2013, 03:57 PM
i have urdu dictionary file txt and csv and xls in unicode i want search any word in dictionary with php?

example word : ابد
or
عابد
or
مستقبل

djr33
03-16-2013, 08:17 PM
If this is a new question, start a new thread.
If this is related to the topic here, explain why, and what the problem is.

Generally speaking, unicode will not be a problem in PHP as long as you have the right character encoding set as every step of the process-- in the HTML, in the encoding of the PHP document itself (the text of the file itself must be saved that way-- you can check in Notepad), in your other text file, and potentially other places like a database.

It'll probably be better to do this in a database if you have a long list, but it's up to you.