cvs: pear /File_SearchReplace SearchReplace.php
| From: | anatoly techtonik | Date: | Fri, 11 Mar 2005 16:46:52 +0000 |
| Subject: | cvs: pear /File_SearchReplace SearchReplace.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-30207@lists.php.net to get a copy of this message | ||
techtonik Fri Mar 11 11:46:52 2005 EDT
Modified files:
/pear/File_SearchReplace SearchReplace.php
Log:
* Fixes for various variants of array/string in s/r pairs for normal mode
* Add arrays support for quick mode
http://cvs.php.net/diff.php/pear/File_SearchReplace/SearchReplace.php?r1=1.7&r2=1.8&ty=u
Index: pear/File_SearchReplace/SearchReplace.php
diff -u pear/File_SearchReplace/SearchReplace.php:1.7 pear/File_SearchReplace/SearchReplace.php:1.8
--- pear/File_SearchReplace/SearchReplace.php:1.7 Fri Mar 11 08:11:35 2005
+++ pear/File_SearchReplace/SearchReplace.php Fri Mar 11 11:46:51 2005
@@ -32,7 +32,7 @@
// | Author: Richard Heyes <richard@phpguru.org> |
// +-----------------------------------------------------------------------+
//
-// $Id: SearchReplace.php,v 1.7 2005/03/11 13:11:35 techtonik Exp $
+// $Id: SearchReplace.php,v 1.8 2005/03/11 16:46:51 techtonik Exp $
//
// Search and Replace Utility
//
@@ -270,7 +270,7 @@
// just for the sake of catching occurences
$local_find = array_values((array) $this->find);
- $local_replace = array_values((array) $this->replace);
+ $local_replace = (is_array($this->replace)) ? array_values($this->replace) :
$this->replace;
for ($i=0; $i < count($file_array); $i++) {
@@ -281,11 +281,16 @@
}
if ($this->php5) {
- $file_array[$i] = str_replace($this->find, $this->replace,
$file_array[$i], &$occurences);
+ $file_array[$i] = str_replace($this->find, $this->replace,
$file_array[$i], &$counted);
+ $occurences += $counted;
} else {
foreach ($local_find as $fk => $ff) {
$occurences += substr_count($file_array[$i], $ff);
- $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : "";
+ if (!is_array($local_replace)) {
+ $fr = $local_replace;
+ } else {
+ $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] :
"";
+ }
$file_array[$i] = str_replace($ff, $fr, $file_array[$i]);
}
}
@@ -313,9 +318,33 @@
clearstatcache();
- $file = fread($fp = fopen($filename, 'r'), filesize($filename));
fclose($fp);
- $occurences = substr_count($file, $this->find);
- $file = str_replace($this->find, $this->replace, $file);
+ $file = fread($fp = fopen($filename, 'r'), filesize($filename));
fclose($fp);
+ $local_find = array_values((array) $this->find);
+ $local_replace = (is_array($this->replace)) ? array_values($this->replace) :
$this->replace;
+
+ $occurences = 0;
+
+ // logic is the same as in str_replace function with one exception:
+ // if <search> is a string and <replacement> is an array - substitution
+ // is done from the first element of array. str_replace in this case
+ // usualy fails with notice and returns "ArrayArrayArray..." string
+ // (this exclusive logic of SearchReplace will not work for php5, though,
+ // because I haven't decided yet whether it is bug or feature)
+
+ if ($this->php5) {
+ $file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i],
&$counted);
+ $occurences += $counted;
+ } else {
+ foreach ($local_find as $fk => $ff) {
+ $occurences += substr_count($file, $ff);
+ if (!is_array($local_replace)) {
+ $fr = $local_replace;
+ } else {
+ $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : "";
+ }
+ $file = str_replace($ff, $fr, $file);
+ }
+ }
if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;