Re: adding lines to a file

From: Date: Mon, 10 Jul 2000 17:09:43 +0000
Subject: Re: adding lines to a file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-5767@lists.php.net to get a copy of this message
Bjorn Sodergren wrote: > > i need to add various lines to the httpd.conf file, via php script. > i use this to search through and find the line that contains > ServerName domain.com , and it returns what line that is. > > $file_httpd = file($httpd_conf); > while( list($no,$line) = each ($file_httpd) ) { > if(preg_match ("/ServerName\s+(.*)/i",$line,$match)){ > if($match[1] == $domain){ > echo $no + 1 ."\n"; //add 1 to make place it on the right > line. > break; > } > } > } > > There is probably a much easier way, but im not that good yet ;) > > now what i need to do, is find the closing </VirtualHost> container, and > then prepend 3 lines right before the closing directive. > > this is a sample, > 1 # -FrontPage- version=4.0 > 2 ## > ... > 427 > 428 <VirtualHost ip> > 429 ServerAdmin webmaster@domain.org > 430 DocumentRoot /home/user/public_html > 431 ServerName domain.org > 432 ServerAlias www.domain.com > 433 User user > 434 Group cust > 435 ErrorLog /home/user/logs/error_log > 436 CustomLog /home/user/logs/access_log common > 437 </VirtualHost> > 438 > 439 .. start of next v host > > i need to instert 3 new lines starting at line 437. > I also noticed that the above expression returns line number > > any ideas ??? say $domain = "apache.com"; Try putting #$domain.com #$domain.com around the virtualhost and then maybe counting inside that. $regexpression = "/#$domain.*#$domain/sUi"; $new_file = preg_replace($regexpression, "#domains $newhtml \n\n#domains", $file); to replace it in $file, then just re write $file. I save all the virtualhost variables in a mysql table, each time its updated I rebuild the httpd.conf from the data in the table. That way all that reg expression stuff isnt so hard. although i still use it to place the virtualhosts in one block. Then each domain can be added,edited,deleted from a web form and my customers can do it themselves and not hassle me.

« previous php.general (#5767) next »