Jump to content

Archived

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

janggu

Search data in text file and display

Recommended Posts

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???

Share this post


Link to post
Share on other sites
[!--quoteo(post=374313:date=May 16 2006, 11:59 AM:name=janggu)--][div class=\'quotetop\']QUOTE(janggu @ May 16 2006, 11:59 AM) [snapback]374313[/snapback][/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.

Share this post


Link to post
Share on other sites
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.

-----------
[b]05/01/06[/b],14.47:41 [b]subject: Request[/b] 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 [b]subject: Information[/b] 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) [snapback]374327[/snapback][/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]

Share this post


Link to post
Share on other sites
Might be a sledge hammer approach, but...

Your text file

[code]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>[/code]

the php file

[code]
<?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
}
?>[/code]

the display

[code]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
)[/code]


Lite...

Share this post


Link to post
Share on other sites
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) [snapback]375028[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Might be a sledge hammer approach, but...

Your text file

[code]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>[/code]

the php file

[code]
<?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
}
?>[/code]

the display

[code]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
)[/code]
Lite...
[/quote]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.