Jump to content

Scan file contents


The Little Guy

Recommended Posts

Line breaks are fine, I am compressing the data before I put it into the file, so line breaks are then removed (when saved). So then when I search, I search compressed data against compressed data. I then uncompress matches.

 

Then how would you expect fgets() to help you? Per the manual it gets 1 line of data. If you have no line breaks then it would, by default, get the entire file content since there are no line breaks. You can specify a length parameter to read the line/file in chunks, but then you're back to the problem of the scenario where the text you are searching for could be split between chunks. I can think of some elaborate means to overcome that, but I'm sure there is a more efficient method.

 

if you were to describe the format of the file (and why you must compress the data) and what the search value would typically look like we might be able to provide some actual help. As of right now this is all hypothetical.

Link to comment
Share on other sites

So I am doing this:

 

Insert the data

$values = array("one", "two", "dog", "mouse", "one", "cat", "mouse");
$handle = fopen("some/file", "ab");
foreach($values as $value){
    $value = "\n".gzcompress($value, 9);
    fwrite($handle, $value);
}
fclose($handle);

 

Search the data

$handle = fopen("some/file", "rb");
$search = gzcompress("mouse", 9);
$results = array();
while (($buffer = fgets($handle)) !== false) {
    $i = 0;
    if($search == ($saved = trim($buffer, "\n"))){
        $results[$i] = gzuncompress($saved);
    }
}
print_r($results);

 

Just an FYI, I am attempting to make a NoSQL database in php (because I was bored).

Link to comment
Share on other sites

Sure, provided you have a *nix based server. shell_exec

 

for reading websites

echo shell_exec(curl -s 'http://forums.phpfreaks.com/index.php?topic=364198.0' | grep 'description' | awk -F 'content="' '{print $2}' | awk -F '"' '{print $1}');

 

for reading files

echo shell_exec(grep 'description' /path/to/file | awk -F 'content="' '{print $2}' | awk -F '"' '{print $1}');

Link to comment
Share on other sites

@jazzman1, by "execute the scripts" i'm assuming you're asking if I've ever used what i described?  The answer is of course, else I would not have posted it.  I didn't test the command in PHP, but through CLI. So it might be broken - but the concept remains

 

BuW7.png

Link to comment
Share on other sites

B/s I've created a bash file and put the script inside it, but I cannot execute the script from php command line.

See what I've done:

#!/usr/bin/php5
echo shell_exec(curl -s 'http://forums.phpfreaks.com/index.php?topic=364198.0' | grep 'description' | awk -F 'content="' '{print $2}' | awk -F '"' '{print $1}');

// php command line

php -f shell_exec.php // echo shell_exec(curl -s 'http://forums.phpfreaks.com/index.php?topic=364198.0' | grep 'description' | awk -F 'content="' '{print $2}' | awk -F '"' '{print $1}')

 

PS. There is something wrong

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.