cvs: pear /File_SearchReplace SearchReplace.php
| From: | anatoly techtonik | Date: | Fri, 11 Mar 2005 10:52:40 +0000 |
| Subject: | cvs: pear /File_SearchReplace SearchReplace.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-30206@lists.php.net to get a copy of this message | ||
techtonik Fri Mar 11 05:52:40 2005 EDT
Modified files:
/pear/File_SearchReplace SearchReplace.php
Log:
* Added PHP5 acceleration in normal mode
* Optimized counter in quick mode
http://cvs.php.net/diff.php/pear/File_SearchReplace/SearchReplace.php?r1=1.5&r2=1.6&ty=u
Index: pear/File_SearchReplace/SearchReplace.php
diff -u pear/File_SearchReplace/SearchReplace.php:1.5 pear/File_SearchReplace/SearchReplace.php:1.6
--- pear/File_SearchReplace/SearchReplace.php:1.5 Thu Mar 10 17:35:17 2005
+++ pear/File_SearchReplace/SearchReplace.php Fri Mar 11 05:52:37 2005
@@ -32,7 +32,7 @@
// | Author: Richard Heyes <richard@phpguru.org> |
// +-----------------------------------------------------------------------+
//
-// $Id: SearchReplace.php,v 1.5 2005/03/10 22:35:17 techtonik Exp $
+// $Id: SearchReplace.php,v 1.6 2005/03/11 10:52:37 techtonik Exp $
//
// Search and Replace Utility
//
@@ -72,11 +72,11 @@
* @param string $replace The string/regex to replace $find with.
* @param array $files The file(s) to perform this operation on.
* @param array $directories (optional) The directories to perform this operation on.
- * @param int $include_subdir If performing on directories, whether to traverse
subdirectories.
+ * @param bool $include_subdir If performing on directories, whether to traverse
subdirectories.
* @param array $ignore_lines Ignore lines beginning with any of the strings in
this array. This
* feature only works with the "normal"
search.
*/
- function File_SearchReplace($find, $replace, $files, $directories = '',
$include_subdir = 1, $ignore_lines = array())
+ function File_SearchReplace($find, $replace, $files, $directories = '',
$include_subdir = TRUE, $ignore_lines = array())
{
$this->find = $find;
@@ -84,7 +84,7 @@
$this->files = $files;
$this->directories = $directories;
$this->include_subdir = $include_subdir;
- $this->ignore_lines = $ignore_lines;
+ $this->ignore_lines = (array) $ignore_lines;
$this->occurences = 0;
$this->search_function = 'search';
@@ -183,7 +183,7 @@
* Accessor for setting include_subdir variable.
*
* @access public
- * @param int $include_subdir Whether to traverse subdirectories or not.
+ * @param bool $include_subdir Whether to traverse subdirectories or not.
*/
function setIncludeSubdir($include_subdir)
{
@@ -259,16 +259,23 @@
$occurences = 0;
$file_array = file($filename);
- for ($i=0; $i<count($file_array); $i++) {
+ if (empty($this->ignore_lines) && substr(PHP_VERSION, 0, 1) == 5) { // PHP5
acceleration
+ str_replace($this->find, $this->replace, $file_array, &$occurences);
- if (count($this->ignore_lines) > 0) {
- for ($j=0; $j<count($this->ignore_lines); $j++) {
- if (substr($file_array[$i],0,strlen($this->ignore_lines[$j])) ==
$this->ignore_lines[$j]) continue 2;
+ } else { // if there are ignore lines or operating in PHP4 - S'n'R string by
string
+ // to catch number of occurences and/or filter strings
+ for ($i=0; $i<count($file_array); $i++) {
+
+ if (count($this->ignore_lines) > 0) {
+ for ($j=0; $j<count($this->ignore_lines); $j++) {
+ if (substr($file_array[$i],0,strlen($this->ignore_lines[$j])) ==
$this->ignore_lines[$j]) continue 2;
+ }
}
- }
- $occurences += substr_count($file_array[$i], $this->find);
- $file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i]);
+ $occurences += substr_count($file_array[$i], $this->find);
+ $file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i]);
+
+ }
}
if ($occurences > 0) $return = array($occurences, implode('', $file_array));
else $return = FALSE;
@@ -293,7 +300,7 @@
clearstatcache();
$file = fread($fp = fopen($filename, 'r'), filesize($filename));
fclose($fp);
- $occurences = count(explode($this->find, $file)) - 1;
+ $occurences = substr_count($file, $this->find);
$file = str_replace($this->find, $this->replace, $file);
if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
@@ -417,7 +424,7 @@
if ($file == '.' OR $file == '..') continue;
if (is_dir($this->directories[$i].$file) == TRUE) {
- if ($this->include_subdir == 1) {
+ if ($this->include_subdir == TRUE) {
$this->directories[] = $this->directories[$i].$file.'/';
continue;
} else {