Re: string replace in files
| From: | Steve Buehler | Date: | Fri, 30 Apr 2004 16:53:22 +0000 |
| Subject: | Re: string replace in files | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-185062@lists.php.net to get a copy of this message | ||
At 11:27 AM 4/30/2004, John W. Holmes wrote:
From: "Steve Buehler" <steve@ibapp.com> #!/usr/bin/php <? $strtoreplace="require \"http://www.domain.com/"; $replacewithstring="require \"/home/domain/www/"; replacestring(); function replacestring(){ GLOBAL $strtoreplace,$replacewithstring;John$FILES=array($content = file_get_contents($value); $content = str_replace($strtoreplace,$replacewithstring,$content); $fp = fopen($value,'w'); fwrite($fp,$content); fclose($fp);ls -1 */*|grep .php); foreach($FILES as $value){ if($value){ echo "file $value\n";} }} ?> You could use glob() to get the list of files, too. $FILES = glob('*.php'); ---John Holmes...
Thank you so much for this. I was thinking of it all wrong when doing this. I have a couple of other questions to go along with this.
1. How would I change this to do a recursive search for the .php files instead of just the *.php or */*.php in the glob?
2. I also have some dns zone files that I was going to modify this script to replace some information in them. What it would have to do is to delete all lines that have white space (could be a space or a tab) NS more white space and then add specific lines that I want in place of the deleted lines.
2.a. There will normally be between 2 and 4 lines that I want deleted and 2 to replace them.
If you don't want to answer this one, that is fine. There are only about 200 zone files and that wouldn't take as long for me to do manually as it would have for the .php file changes.
Thanks
Steve