• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Reading a flat text database in php

rapmaster

New Member
ok, I cant figure this out for myself because its not the same in php as it is in perl.

Ok, I have a flat text database, its not a big database, it has only one logged item so it looks like this:

logged1
logged2
logged3
logged4
logged5


I need to search through those entrys and match an entered variable to the data, so:

if entereddata=logged1
count++

that kind thing. Can I read a flat database in PHP with the split command? Or is that not avalible in PHP? Please help.
 
What do you want to do ??
for spilt stuff from txt data file, you can you some regex, something like :
PHP:
$split = preg_split("_=_",join('',file(data.txt")));
here you split everything with _=_ and then join them all with join() and file() count each line from the file to an array.
More info in www.php.net ,search for this FUNC.
 
ok, let me show you how its done in CGI, I need to be able to do the same basic thing in PHP

Code:
$thedb = "data.txt";
$search = <STDIN>;
open($thedb);
$match = 0;

while(<$thedb>) {
$the_data = $_;
chomp($the_data);
($loggeditem) =
split(/\n/, $thedata);

if($search eq $loggeditem) {
$match = $match +1;
}

print "There were $match matches to the search";

So, breaking it down, it needs to be able to open the flat database, parse the info in the database. Search through the parsed data, count the number of successful hits, and print the number of successful hits.
 
rapmaster, you mean Perl. CGI can be written in Python, PHP, Perl, C++, etc.

megapuzik, chomp() removes the last character from strings if it is a newline.

"asdfasdf\n\tasdfawsdfadsf\n" -> "asdfasdf\n\tasdfawsdfadsf"
 
OK ->
PHP:
<?php
$data = "file.txt" ;
$datafile = fopen($data);
if(preg_match($string,file($datafile)))
{
print "ok";
}
else
{
print "no";
}
?>
file() will convert each line in the txt file to an array, there is also split() and join() [and explode() and implode()] seach in the manual. (www.php.net)

In this example, if you want to print the results, just put the regex in a var and print the var.
 
Back
Top