Quote:
Originally Posted by nomb
Correct, I am having the regex search the entire file and find the Port setting. When you use '^' saying the start must match, then if the found text isn't the very first thing in the file it wont match. When I went to use '$' it would not let me put anything after the first line it was suppose to match.
|
I believe that this is not a problem with the regular expression per se...
Here's some mock up code that I wrote...
Code:
#! /usr/bin/php
<?php
$YourFile = "sshd2.conf";
$handle = fopen($YourFile, 'r');
while (!feof($handle)) {
$Data = fgets($handle, 256);
if( ! preg_match ( '/^#/', $Data) ){
if( preg_match('/port\s+22/', $Data) ){
Print $Data;
}
}
}
fclose($handle);
?>
here's sshd2.conf:
Code:
###########################################################################
# $Id: sshd2.conf,v 1.7 2005/02/24 17:05:20 kirk Exp $
###########################################################################
# You can put comments anywhere you want to. They are effective for the
# rest of the line.
# this is in the format of <name> = <value>. Whitespace at the beginning
# and end of the lines is removed. Whitespace before and after the = sign
# is removed. Everything is case *insensitive*.
# Yes = True = On = 1
# No = False = Off = 0
Title = "Sshd2"
#port 333
port 22
# Which logfile group...
LogFile = messages
# Only give lines pertaining to the sshd2 service...
*OnlyService = sshd2
*RemoveHeaders
########################################################
# This was written and is maintained by:
# Kirk Bauer <kirk@kaybee.org>
#
# Please send all comments, suggestions, bug reports,
# etc, to kirk@kaybee.org.
########################################################
# vi: shiftwidth=3 tabstop=3 et
This yields
I believe that the problem that you're having is that the file is being put into a single variable. You're doing the regex match once, and getting a single match... what you really need to do is split the variable into individual lines -- into an array, then iterate through the array, doing a regex match on each element of the array.
I'm going to leave that to you as an exersize -- mostly because I don't speak PHP ;-)