Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REGEX - what the....?

Status
Not open for further replies.

Knackers

Technical User
Apr 26, 2001
59
0
0
AU
Hi,

I am writing a script that simulates a web server. It accepts a request header and makes the appropriate response etc.

I want to impliment Basic Authentication, using dynamic variables from a .htaccess file (AuthType,AuthName). Problem is that the .htaccess file that I am working from is littered with commenting, which cannot be changed.

What I would like to know is how can I extract the two uncommented lines in the below file? What I need to be able to access is the word "Basic" (without the quotes) and "Some realm" (with the quotes). Please also note that the two required lines are indented, I think with a tab.
----------------------------------------------------
Code:
# AuthType specifies that Basic authentication must 
# be used on all filesin this directory.
# AuthName specifies the name of the realm for which 
# authentication is required.

   AuthType Basic
   AuthName "Some realm"
   # AuthUserFile apache2/htpasswd.users
   # Require valid-user

# Check for the existence of the .htaccess file,
# then check if AuthType and AuthName are not commented 
# out. Then it must use the AuthName value when it 
# implements basic authentication.
----------------------------------------------------

What I have been thinking is that I would need to read through the file line by line, checking for whitespace at the begining of the line, or checking for the words "AuthType" or "AuthName". Any help with this would be greatly appreciated.

Cheers
 
Test each line from the file with this perl-compatible regular expression:

[tt]
Code:
preg_match('/^\s*#|^\s*$/', $line_from_file)
[/tt]

And process everything that it doesn't match ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top