Jump to content


Photo

Search data in text file and display


  • Please log in to reply
5 replies to this topic

#1 janggu

janggu
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 16 May 2006 - 03:59 PM

Hello,

Does anyone know if it is possible to search data in text file and get result back with PHP? The search function will have to look for a specific word in a text file and dispay a list records that contain the word.

Or, should I export this text file into MySQL and manipulate data from there? Any thought???

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 16 May 2006 - 04:17 PM

[!--quoteo(post=374313:date=May 16 2006, 11:59 AM:name=janggu)--][div class=\'quotetop\']QUOTE(janggu @ May 16 2006, 11:59 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hello,

Does anyone know if it is possible to search data in text file and get result back with PHP? The search function will have to look for a specific word in a text file and dispay a list records that contain the word.

Or, should I export this text file into MySQL and manipulate data from there? Any thought???
[/quote]

it is fairly simple to read a text file and search the contents using PHP. the best method would most likely be to read the file line by line (assuming you have one record per line) and simply displaying the lines that have a match. could you be more specific in what you are after? for instance, give us an example of how your file is formatted and what you might be searching for. once we have that information, we may be able to help you with the details.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 janggu

janggu
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 18 May 2006 - 04:03 PM

I am sorry for late response. Apparently, each record is ended by <br> tag and the search will be performed by date and subject. Below is how records are structure in text file.

-----------
05/01/06,14.47:41 subject: Request received from mail@hotmail.com<br>05/01/06,14.47:41 subject: Request received from mail@hotmail.com<br>05/01/06,14.47:41 subject: Information received from mail@hotmail.com<br>
-----------
Is it enough information to start?

Thanks so much!!!

[!--quoteo(post=374327:date=May 16 2006, 04:17 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ May 16 2006, 04:17 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
it is fairly simple to read a text file and search the contents using PHP. the best method would most likely be to read the file line by line (assuming you have one record per line) and simply displaying the lines that have a match. could you be more specific in what you are after? for instance, give us an example of how your file is formatted and what you might be searching for. once we have that information, we may be able to help you with the details.
[/quote]


#4 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 18 May 2006 - 06:30 PM

Might be a sledge hammer approach, but...

Your text file

05/01/06,14.47:41 subject: Request received from mail@hotmail.com<br>05/01/06,14.47:41 subject: Request received from mail@hotmail.com<br>05/01/06,14.47:41 subject: Information received from mail@hotmail.com<br>

the php file

<?PHP
$file = "some.txt";
$fp = f open($file, 'r');
$contents = f read($fp, filesize($file));
f close($fp);
$needle="subject:";
$delimiter = "|";
$contents = str_replace($needle,$delimiter,$contents);
$needle="received from";
$delimiter = "|";
$contents = str_replace($needle,$delimiter,$contents);
$new_array = explode("<br>",$contents);
$count = count($new_array);
if(strlen(trim($new_array[$count-1]))<1) {
array_pop($new_array);
}
$count = count($new_array);
$i=0;
for($i=0;$i<$count;$i++){
  $next_array[$i] = explode("|",$new_array[$i]);
?>
<Pre>
<?PHP

  print_r($next_array[$i]);
?>
</pre>
<?PHP
}
?>

the display

Array
(
    [0] => 05/01/06,14.47:41 
    [1] =>  Request 
    [2] =>  mail@hotmail.com
)

Array
(
    [0] => 05/01/06,14.47:41 
    [1] =>  Request 
    [2] =>  mail@hotmail.com
)

Array
(
    [0] => 05/01/06,14.47:41 
    [1] =>  Information 
    [2] =>  mail@hotmail.com
)


Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#5 janggu

janggu
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 18 May 2006 - 07:12 PM

Thanks for your help but how can I add a condition? For instance,

Select record between '05/01/06' to '05/10/06' and/or subject = 'Information'

Is this possible? Perhaps, there is a PHP function???

Thanks again!


[!--quoteo(post=375028:date=May 18 2006, 06:30 PM:name=litebearer)--][div class=\'quotetop\']QUOTE(litebearer @ May 18 2006, 06:30 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Might be a sledge hammer approach, but...

Your text file

05/01/06,14.47:41 subject: Request received from mail@hotmail.com<br>05/01/06,14.47:41 subject: Request received from mail@hotmail.com<br>05/01/06,14.47:41 subject: Information received from mail@hotmail.com<br>

the php file

<?PHP
$file = "some.txt";
$fp = f open($file, 'r');
$contents = f read($fp, filesize($file));
f close($fp);
$needle="subject:";
$delimiter = "|";
$contents = str_replace($needle,$delimiter,$contents);
$needle="received from";
$delimiter = "|";
$contents = str_replace($needle,$delimiter,$contents);
$new_array = explode("<br>",$contents);
$count = count($new_array);
if(strlen(trim($new_array[$count-1]))<1) {
array_pop($new_array);
}
$count = count($new_array);
$i=0;
for($i=0;$i<$count;$i++){
  $next_array[$i] = explode("|",$new_array[$i]);
?>
<Pre>
<?PHP

  print_r($next_array[$i]);
?>
</pre>
<?PHP
}
?>

the display

Array
(
    [0] => 05/01/06,14.47:41 
    [1] =>  Request 
    [2] =>  mail@hotmail.com
)

Array
(
    [0] => 05/01/06,14.47:41 
    [1] =>  Request 
    [2] =>  mail@hotmail.com
)

Array
(
    [0] => 05/01/06,14.47:41 
    [1] =>  Information 
    [2] =>  mail@hotmail.com
)
Lite...
[/quote]


#6 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 18 May 2006 - 10:42 PM

Hmmm...

To use a date as a possible search criteria, you will need to 'expand' the arrays a bit. IF each date is consistent in its format (ie MM/DD/YY) you could insert another pipe "|" to make the date an element of the array. The same holds true for the other search criteria you might want to use.

Bottom line is, it would be faster and easier to use a database (mysql etc).

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users