The Little Guy Posted August 21, 2012 Share Posted August 21, 2012 Is it possible to scan for a string within a file without reading the file into a variable? Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/ Share on other sites More sharing options...
xyph Posted August 21, 2012 Share Posted August 21, 2012 Short answer, no. You can read 'chunks' of a file into memory at a time, and search within the chunk. If for some reason a match is split between chunks though, you may not get correct results. Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371275 Share on other sites More sharing options...
The Little Guy Posted August 21, 2012 Author Share Posted August 21, 2012 what about fgets? Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371277 Share on other sites More sharing options...
xyph Posted August 21, 2012 Share Posted August 21, 2012 What happens if the search term includes a line-break? Or if the term is two words, and they happen to reside at the end of one line, and the start of the other? Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371280 Share on other sites More sharing options...
The Little Guy Posted August 22, 2012 Author Share Posted August 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371299 Share on other sites More sharing options...
Psycho Posted August 22, 2012 Share Posted August 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371301 Share on other sites More sharing options...
The Little Guy Posted August 22, 2012 Author Share Posted August 22, 2012 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). Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371304 Share on other sites More sharing options...
Mahngiel Posted August 22, 2012 Share Posted August 22, 2012 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}'); Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371308 Share on other sites More sharing options...
jazzman1 Posted August 22, 2012 Share Posted August 22, 2012 @Mahngiel, have you ever tried to execute the scripts? Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371311 Share on other sites More sharing options...
Mahngiel Posted August 22, 2012 Share Posted August 22, 2012 @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 Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371312 Share on other sites More sharing options...
jazzman1 Posted August 22, 2012 Share Posted August 22, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371313 Share on other sites More sharing options...
Mahngiel Posted August 22, 2012 Share Posted August 22, 2012 code used for screenshot: 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}'"); Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371314 Share on other sites More sharing options...
jazzman1 Posted August 22, 2012 Share Posted August 22, 2012 Ok, I'm doing something wrong. I am going to post a result later Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371315 Share on other sites More sharing options...
jazzman1 Posted August 22, 2012 Share Posted August 22, 2012 Kill me with something heavy. I was forget to put the php open tags in my bash file Everything is fine, I got a result. Nice response. Quote Link to comment https://forums.phpfreaks.com/topic/267396-scan-file-contents/#findComment-1371316 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.