Skip to content

Commit

Permalink
add anchors when constructing pattern from a non-regex oldline
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris--S committed May 29, 2015
1 parent 4b8290f commit 3dfe7d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions _test/tests/inc/io_replaceinfile.test.php
Expand Up @@ -79,4 +79,17 @@ function test_edgecase2() {
$this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\012", false, -1));
$this->assertEquals("The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file), "Edge case: new line the same as old line");
}

/**
*
*/
function test_edgecase3()
{
$file = TMP_DIR . '/test.txt';
$contents = "The\012Delete\01201Delete\01202Delete\012Test\012";
// Replace all, no regex, oldline exactly matches one line; matches part of other lines - only the exact match should be replaced
io_saveFile($file, $contents);
$this->assertTrue(io_replaceInFile($file, "Delete\012", "Replace\012", false, -1));
$this->assertEquals("The\012Replace\01201Delete\01202Delete\012Test\012", io_readFile($file), "Edge case: old line is a match for parts of other lines");
}
}
2 changes: 1 addition & 1 deletion inc/io.php
Expand Up @@ -312,7 +312,7 @@ function io_replaceInFile($file, $oldline, $newline, $regex=false, $maxlines=0)
}

// make non-regexes into regexes
$pattern = $regex ? $oldline : '/'.preg_quote($oldline,'/').'/';
$pattern = $regex ? $oldline : '/^'.preg_quote($oldline,'/').'$/';
$replace = $regex ? $newline : addcslashes($newline, '\$');

// remove matching lines
Expand Down

0 comments on commit 3dfe7d6

Please sign in to comment.