diff options
author | Patrick Brown <ptbrown@whoopdedo.org> | 2015-05-08 17:11:44 -0400 |
---|---|---|
committer | Patrick Brown <ptbrown@whoopdedo.org> | 2015-05-08 17:11:44 -0400 |
commit | 6c0002048504e43b399abece0668afa2b5c87a07 (patch) | |
tree | 39122fc8451db991988f1ff6fd211f5a90d9c5b0 /inc | |
parent | d0d224a82eddf30aa6daead4796cb10dd0466767 (diff) | |
download | rpg-6c0002048504e43b399abece0668afa2b5c87a07.tar.gz rpg-6c0002048504e43b399abece0668afa2b5c87a07.tar.bz2 |
Limit number of lines to replace.
Diffstat (limited to 'inc')
-rw-r--r-- | inc/io.php | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/inc/io.php b/inc/io.php index 11a3fa77f..dbb42114b 100644 --- a/inc/io.php +++ b/inc/io.php @@ -313,10 +313,16 @@ function io_replaceInFile($file, $oldline, $newline, $regex=false, $maxlines=0) // remove all matching lines if ($regex) { - if($maxlines == 0) { - $lines = preg_grep($oldline, $lines, PREG_GREP_INVERT); + if($maxlines > 0) { + $matches = preg_grep($oldline, $lines); + $count = 0; + foreach($matches as $ix=>$m) { + $lines[$ix] = preg_replace($oldline, $newline, $m); + if(++$count >= $maxlines) break; + } } else { - $lines = preg_replace($oldline, $newline, $lines, $maxlines); + $lines = ($maxlines == 0) ? preg_grep($oldline, $lines, PREG_GREP_INVERT) + : preg_replace($oldline, $newline, $lines, $maxlines); } } else { $count = 0; |