unkwntech Posted July 23, 2008 Share Posted July 23, 2008 [code]I just wrote this however, it keeps returning: [code] Array ( [0] => ) Here is the script: #!/usr/bin/php -q <?php error_reporting(E_ALL); $handle = fopen("/etc/squid/squid.conf", "r+"); $out = array(); if ($handle) { while(!feof($handle)) { $buffer = fgets($handle); $buffer = str_split($buffer); if($buffer['0'] != '#') { $out = implode('', $buffer) . '\r\n'; print_r($buffer); exit; } } fwrite($handle, $dataOut); fclose($handle); } ?> And here is an example of the first few lines of the file that is being read in. # message to cache.log. You can allow responses from unknown # nameservers by setting this option to 'off'. # #Default: # ignore_unknown_nameservers on # TAG: digest_generation # This controls whether the server will generate a Cache Digest # of its contents. By default, Cache Digest generation is # enabled if Squid is compiled with USE_CACHE_DIGESTS defined. # #Default: # digest_generation on Of course there are lines without a preceding # What we are trying to do here is strip all the comments from a few config files. All the comments start with a #. The file I'm testing with is 3118 lines long, and only has 12 lines that aren't comments.[/code][/code] Link to comment https://forums.phpfreaks.com/topic/116110-solved-script-not-working/ Share on other sites More sharing options...
jonsjava Posted July 23, 2008 Share Posted July 23, 2008 this script will scrub out all lines that contain a "#" in it. Regardless of its location on the line. #!/usr/bin/php -q <?php error_reporting(E_ALL); $file = "/etc/squid/squid.conf"; $handle = fopen($file, "r+"); $data = fread($handle, filesize($file)); fclose($handle); $data_array = explode("\n", $data); $new_data = ""; foreach ($data_array as $value){ if (!strstr($value, "#")){ $new_data .= $value."\n"; } } print $new_data; I have it printing out the results to the screen for now, but you can change that. Link to comment https://forums.phpfreaks.com/topic/116110-solved-script-not-working/#findComment-597070 Share on other sites More sharing options...
unkwntech Posted July 23, 2008 Author Share Posted July 23, 2008 Thanks for the response do you know why mine was not working? As I understand it fgets() should read in each line individualy, in which case this should work. Am I misunderstaning this or is this one of 'those things'? Link to comment https://forums.phpfreaks.com/topic/116110-solved-script-not-working/#findComment-597077 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.