Re: string replace in files
| From: | John W. Holmes | Date: | Fri, 30 Apr 2004 16:27:19 +0000 |
| Subject: | Re: string replace in files | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-185058@lists.php.net to get a copy of this message | ||
From: "Steve Buehler" <steve@ibapp.com>
> I am trying to write a script that will replace a string in a
> file. Actually, about 3000 files. And I am stuck. I can get the list of
> files, but then I pretty much become stuck. I wanted to try str_ireplace
> (not sure if that is what I should use or not), but I can't even get far
> enough to do that. I am hoping that someone out there has an easy
response
> to this. Below is the code that I have so far.
> Thanks
> Steve
>
> #!/usr/bin/php
> <?
> $strtoreplace="require
> \"http://www.domain.com/";
> $replacewithstring="require \"/home/domain/www/";
>
> replacestring();
>
> function replacestring(){
> GLOBAL $strtoreplace,$replacewithstring;
> $FILES=array(
ls -1 */*|grep .php);
> foreach($FILES as $value){
> if($value){
> echo "file $value\n";
$content = file_get_contents($value);
$content = str_replace($strtoreplace,$replacewithstring,$content);
$fp = fopen($value,'w');
fwrite($fp,$content);
fclose($fp);
> }
> }
> }
> ?>
You could use glob() to get the list of files, too.
$FILES = glob('*.php');
---John Holmes...