Jump to content

How to search a word in text file.


pankaj.ghadge

Recommended Posts

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.logFile2.logFile3.logFile4.logWhen 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).Please guide me ............................................

Link to comment
Share on other sites

places to startyou want to send that value in the text field to the server...so you have to submit the formunless you are going to be doing an ajax call you really don't need that onclick eventfigure what regular expressions you will useuse php to read all files in the directory

if ($handle = opendir('/path/to/files')) { echo "Directory: $handle\n"; 	echo "Files:\n"; 	while (($file = readdir($handle))) { 		echo "$file\n"; 	} 	closedir($handle); }

within that while loop you'll need it to read through the file and search for a match to your regular expressionpersonally I stink at writing reg expressions but i'm sure some else can help you here if after reading up on them you still have a struggle with it...hope this helps

Link to comment
Share on other sites

Well, I got a little curious. :) What you need is in the PHP section. The rest just pretties it up.search.php

<?php	if (!empty($_POST['keywords'])) {				$words = explode(" ", $_POST['keywords']);		$len = count ($words);				// THIS SETS THE DIRECTORY TO SEARCH IN		$dirpath = '.';				$D = opendir($dirpath);		if (!$D) {			die ("Can't open directory");		}		while ($file = readdir($D)) {			if (is_file("$dirpath/$file")) {				$F = fopen("$dirpath/$file", "r");				$data = fread($F, filesize("$dirpath/$file"));				$counter = 0;				foreach ($words as $word) {					$pattern = "/\b$word\b/i";					if (preg_match($pattern, $data) ) {						$counter++;					}					if ($counter == $len) {						$filearray[] = $file;					}				}				fclose($F);			}		}		closedir($D);	}?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd"><html>	<head>		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">		<title></title>		<style type="text/css">			* {				margin: 0;				padding: 0;			}			div.container {				width: 400px;				min-height: 95%;				margin: 4% auto 0 auto;			}			div.results {				margin-top: 2em;				border: solid 1px #000000;				width: 20em;				min-height: 1em;				padding: 12px;			}			h2 {				margin-bottom: 1em;				text-align: center;			}		</style>	</head>	<body>		<div class="container">			<div>				<form action="search.php" method="post">					<label>Search: </label><input type="text" name="keywords">				</form>			</div>			<div class="results">				<h2>Results</h2>				<?php					if (!empty ($filearray) ) {						foreach ($filearray as $value) {							echo $value . '<br>';						}					}				?>			</div>		</div>	</body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...