Search in Text File

Search a text in a text file, return the whole line

Page 1 of 1

5 Replies - 20434 Views - Last Post: 21 March 2010 - 08:49 AM Rate Topic: -----

#1 Guest_rhj*


Reputation:

Search in Text File

Posted 19 March 2010 - 03:24 PM

Hi,

I am new to PHP and I want to search a text in a text file in a line and return the whole line
Can someone please explain how I can do this? If I use fgets will this function read every character one by one?

Thank you
Is This A Good Question/Topic? 0

Replies To: Search in Text File

#2 KingCuddles   User is offline

  • D.I.C Regular

Reputation: 176
  • View blog
  • Posts: 496
  • Joined: 20-December 08

Re: Search in Text File

Posted 19 March 2010 - 03:33 PM

Quote

At DIC we like to see evidence of a thread starter having done some work beforehand or at least show some basic knowledge of the language and methodology, and tend not to help people who just post a question essentially asking for code.


Google is your friend, a quick search will tell you everything you need to get started.

Search "PHP Like" to get started.
Was This Post Helpful? 0
  • +
  • -

#3 rhjamal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-March 10

Re: Search in Text File

Posted 19 March 2010 - 04:53 PM

I have written this code, but my problem is how can I return the whole line where the text is found?

$file = file_get_contents("Message.txt");
	$SearchString = "$Search";
	$breakstrings = explode('/',$SearchString);

	foreach ($breakstrings as $values)
	{
		if(!strpos($file, $values))
		{
			echo $values." string not found!\n";
		}
		else
		{
			echo $values." string Found!\n";
		}

This post has been edited by JackOfAllTrades: 19 March 2010 - 06:45 PM
Reason for edit:: Added code tags.

Was This Post Helpful? 0
  • +
  • -

#4 mscobb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 20-March 10

Re: Search in Text File

Posted 20 March 2010 - 10:47 PM

Yes you can use fgets to read a line and print it out. here is a very simple example of how to do so hopefully this will help guide you in the right direction.

<?php

$handle = fopen("file.txt", "r");  // use r in the handle if you are only reading the file
while (!feof($handle)) {          // while not at the end of the file
     $text = fgets(handle);
     echo $text, "<br />";
}// end of while
fclose($handle);

?>



Was This Post Helpful? 1
  • +
  • -

#5 hadi_php   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 382
  • Joined: 23-August 08

Re: Search in Text File

Posted 21 March 2010 - 08:26 AM

you can also use regular expression for exact search...
Was This Post Helpful? 0
  • +
  • -

#6 mscobb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 20-March 10

Re: Search in Text File

Posted 21 March 2010 - 08:49 AM

hadi's right too. Say you want to find the word php in a text file you use the preg_match function to check it kinda like this..

if(preg_match('/php/i', $Text))  {   // declare a variable before hand to handle the file or text you are searching and this should check it.
echo 'we have found the word php in the text';

else {
echo 'we did not find the word php in your text';
   }
}




more examples of other regular expressions and the use of preg_match can be found on this site just search for them, Google is your friend.

This post has been edited by mscobb: 21 March 2010 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1