Find and replace in multiple files

OK, the best solution in php is to search through a bunch of file contents for a specific line and replace it with something else.

Just like notepad ++ does, but obviously I don't need an interface for this.

+6
php
source share
2 answers
foreach (glob("path/to/files/*.txt") as $filename) { $file = file_get_contents($filename); file_put_contents($filename, preg_replace("/regexhere/","replacement",$file)); } 
+22
source share

So, I recently encountered a problem in which our web host was converting from PHP 5.2 to 5.3, and in the process it violated our Magento installation. I made some custom settings that were suggested, but found that there are still some broken areas. I realized that most of the problems are related to the problem with the "toString" function present in Magento, and now the obsolete PHP sharing function. Seeing this, I decided that I would try to create code that would find and replace all the different cases of broken functions. I managed to succeed in creating this feature, but, unfortunately, the β€œshot” approach did not work. I still had errors. However, I feel that the code has great potential, and I wanted to publish what I came up with.

Please use this with caution. I would recommend backing up your files so that you can restore them from a backup if you have any problems.

In addition, you do not necessarily want to use it as is. I provide the code as an example. You probably want to change what is replaced.

How the code works, it can find and replace everything that is in the folder in which it is placed, and in subfolders. I configured it so that it will only search files with the PHP extension, but you can change it as needed. As it searches, it will list which files it has changed. To use this code, save it as "ChangePHPText.php" and load this file where you need the changes. You can then launch it by loading the page associated with this name. For example, mywebsite.com \ ChangePHPText.php.

  <?php ## Function toString to invoke and split to explode function FixPHPText( $dir = "./" ){ $d = new RecursiveDirectoryIterator( $dir ); foreach( new RecursiveIteratorIterator( $d, 1 ) as $path ){ if( is_file( $path ) && substr($path, -3)=='php' && substr($path, -17) != 'ChangePHPText.php'){ $orig_file = file_get_contents($path); $new_file = str_replace("toString(", "invoke(",$orig_file); $new_file = str_replace(" split(", " preg_split(",$new_file); $new_file = str_replace("(split(", "(preg_split(",$new_file); if($orig_file != $new_file){ file_put_contents($path, $new_file); echo "$path updated<br/>"; } } } } echo "----------------------- PHP Text Fix START -------------------------<br/>"; $start = (float) array_sum(explode(' ',microtime())); echo "<br/>*************** Updating PHP Files ***************<br/>"; echo "Changing all PHP containing toString to invoke and split to explode<br/>"; FixPHPText( "." ); $end = (float) array_sum(explode(' ',microtime())); echo "<br/>------------------- PHP Text Fix COMPLETED in:". sprintf("%.4f", ($end-$start))." seconds ------------------<br/>"; ?> 
+2
source share

All Articles