views:

150

answers:

1

Consider this:

$sServerPath = "\\\\nlyehvedw1cl016\\projects$\\ARCLE_SW_SVN\\";
$sSVNParentPath = $sServerPath."svn\\";

$bla = "
authz_module_name = TEST_TestRepos
repository_dir = bla

W";
$sSVNParentPath = $sServerPath."svn\\";
$sReplaceBy =  "repository_dir = ".$sSVNParentPath.$sProjectName."\n";
echo $sReplaceBy;
echo preg_replace ('/repository_dir = ([a-zA-Z0-9\/].*?)\n/i', $sReplaceBy, $bla);

The result is:

repository_dir = \\nlyehvedw1cl016\projects$\ARCLE_SW_SVN\svn\

authz_module_name = TEST_TestRepos
repository_dir = \nlyehvedw1cl016\projects$\ARCLE_SW_SVN\svn\

W

The echo of $sReplaceBy shows the resulting string as I expect it, including the first 2 back-slashes. However, after the preg_replace, the echo of the result shows only one back-slash!

Anybody know what's causing this?

A: 

From PHP docs:

To use backslash in replacement, it must be doubled ("\\" PHP string).

Since your replacement doesn't contain quotes, you can simply use addslashes():

echo preg_replace ('/repository_dir = ([a-zA-Z0-9\/].*?)\n/i', addslashes($sReplaceBy), $bla);
reko_t
Found it indeed in the explanation of the replacement of preg_replace on php.net....I have read this, but apparently with eyes closed....
Hoppie