Jump to: navigation, search

PHP File Search within file

From w3cyberlearnings

Contents

PHP Search within a file

Search within a file to look for a specific word.

File Content(test1.txt)

a good person make a good world and nothing is better than them.
a good person loves to make peace.
a good person mostly don't make a lot of problem.
a good person likes to say good word.
nothing is a bad person unless she or he loves to say bad.
forget to tell you how a good person loves peace.

Example 1

<?php

$searchword = "nothing";
$matches = array();

$handle = fopen("test1.txt", "r");
if ($handle) {
	while (!feof($handle)) {
		$buffer = fgets($handle);
		if (strpos($buffer, $searchword) !== FALSE)
			$matches[] = $buffer;
	}
	fclose($handle);
}

echo 'I found :<br/>';
echo join('<br/>',$matches);
?>

Output

I found :
a good person make a good world and nothing is better than them. 
nothing is a bad person unless she or he loves to say bad.

Related Links


  1. PHP File Linux vs. Windows
  2. PHP File Information
  3. PHP File Create
  4. PHP File Close
  5. PHP File Read
  6. PHP File Read Reverse
  1. PHP File Write
  2. PHP File Write to end or append to
  3. PHP File Write at Beginning
  4. PHP File Write at specific location
  5. PHP File Truncate
  6. PHP File Replace a specific word
  1. PHP File Replace a specific word with associative array
  2. PHP File Copy File
  3. PHP File Copy Reverse
  4. PHP File Search within file
  5. PHP File Delete File
  6. PHP File Template
  1. PHP File Email Template
  2. PHP File Template with MySQL
  3. PHP File Access with MySQL
Navigation
Web
SQL
MISC
References