Jump to content

[SOLVED] Script not working


unkwntech

Recommended Posts

[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

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.

Archived

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

×
×
  • 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.