title: Parse Apache VHosts to get list of Domains and Folder Paths
body: <p>Given a path X or a copy of the apache vhosts configuration file, how would I parse that file using PHP?</p>
  
  <p>E.g. Given a variable containing a string that has the contents of an Apache vhosts configuration, how would I get a list of hosted domains/subdomains alias?</p>
  
  <p>For example, given:</p>
  
  <pre><code>#
  # Use name-based virtual hosting.
  #
  NameVirtualHost *:80
  
  
  
  &lt;VirtualHost *:80&gt;
      ServerAdmin contact@tomjn.com
      DocumentRoot "/srv/www/localhost/
  
      ServerName 127.0.0.1
      ServerAlias localhost
      CustomLog "/srv/www/logs/localhost-access_log.log" combined
      ErrorLog "/srv/www/logs/localhost-error_log.log"
  
      &lt;Directory "/srv/www/localhost"&gt;
          Options Indexes FollowSymLinks Includes ExecCGI
          AllowOverride All
          Order allow,deny
          Allow from all
      &lt;/Directory&gt;
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:80&gt;
      ServerAdmin contact@tomjn.com
      DocumentRoot "/srv/www/2.7.localhost.com/
  
      ServerName 2.7.localhost.com
      ServerAlias 2.7.localhost.com
      CustomLog "/srv/www/logs/2.7.localhost.com-access_log.log" combined
      ErrorLog "/srv/www/logs/2.7.localhost.com-error_log.log"
  
      &lt;Directory "/srv/www/2.7.localhost.com"&gt;
          Options Indexes FollowSymLinks Includes ExecCGI
          AllowOverride All
          Order allow,deny
          Allow from all
      &lt;/Directory&gt;
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>How would I get this output:</p>
  
  <ul>
  <li>localhost</li>
  <li>2.7.localhost.com</li>
  </ul>
  
  <p>Here's something very close written in Python:</p>
  
  <p><a href="http://www.poldylicious.de/system/files/apacheconfig.py.txt" rel="nofollow">http://www.poldylicious.de/system/files/apacheconfig.py.txt</a></p>