cvs: pear /File_SearchReplace SearchReplace.php

From: Date: Fri, 11 Mar 2005 13:11:39 +0000
Subject: cvs: pear /File_SearchReplace SearchReplace.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-30204@lists.php.net to get a copy of this message
techtonik Fri Mar 11 08:11:39 2005 EDT Modified files: /pear/File_SearchReplace SearchReplace.php Log: * Add support for array search and replace arguments in normal mode http://cvs.php.net/diff.php/pear/File_SearchReplace/SearchReplace.php?r1=1.6&r2=1.7&ty=u Index: pear/File_SearchReplace/SearchReplace.php diff -u pear/File_SearchReplace/SearchReplace.php:1.6 pear/File_SearchReplace/SearchReplace.php:1.7 --- pear/File_SearchReplace/SearchReplace.php:1.6 Fri Mar 11 05:52:37 2005 +++ pear/File_SearchReplace/SearchReplace.php Fri Mar 11 08:11:35 2005 @@ -32,7 +32,7 @@ // | Author: Richard Heyes <richard@phpguru.org> | // +-----------------------------------------------------------------------+ // -// $Id: SearchReplace.php,v 1.6 2005/03/11 10:52:37 techtonik Exp $ +// $Id: SearchReplace.php,v 1.7 2005/03/11 13:11:35 techtonik Exp $ // // Search and Replace Utility // @@ -59,6 +59,7 @@ var $ignore_sep; var $occurences; var $search_function; + var $php5; var $last_error; // }}} @@ -88,6 +89,7 @@ $this->occurences = 0; $this->search_function = 'search'; + $this->php5 = (substr(PHP_VERSION, 0, 1) == 5) ? TRUE : FALSE; $this->last_error = ''; } @@ -259,22 +261,34 @@ $occurences = 0; $file_array = file($filename); - if (empty($this->ignore_lines) && substr(PHP_VERSION, 0, 1) == 5) { // PHP5 acceleration - str_replace($this->find, $this->replace, $file_array, &$occurences); + if (empty($this->ignore_lines) && $this->php5) { // PHP5 acceleration + $file_array = str_replace($this->find, $this->replace, $file_array, &$occurences); - } 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++) { + } else { // str_replace() doesn't return number of occurences in PHP4 + // so we need to count them manually and/or filter strings + $ignore_lines_num = count($this->ignore_lines); - if (count($this->ignore_lines) > 0) { - for ($j=0; $j<count($this->ignore_lines); $j++) { + // just for the sake of catching occurences + $local_find = array_values((array) $this->find); + $local_replace = array_values((array) $this->replace); + + for ($i=0; $i < count($file_array); $i++) { + + if ($ignore_lines_num > 0) { + for ($j=0; $j < $ignore_lines_num; $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]); - + if ($this->php5) { + $file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i], &$occurences); + } else { + foreach ($local_find as $fk => $ff) { + $occurences += substr_count($file_array[$i], $ff); + $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : ""; + $file_array[$i] = str_replace($ff, $fr, $file_array[$i]); + } + } } }

« previous php.pear.cvs (#30204) next »