开发者

preg_replace removes second backslash at start of line

开发者 https://www.devze.com 2022-12-09 14:47 出处:网络
Consider this: $sServerPath = \"\\\\\\\\nlyehvedw1cl016\\\\projects$\\\\ARCLE_SW_SVN\\\\\"; $sSVNParentPath = $sServerPath.\"svn\\\\\";

Consider this:

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

$bla = "
authz_module_name = TEST_TestRepos
repository_dir = bla

W";
$sSVNParentPath = $sServerPath."sv开发者_运维百科n\\";
$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?


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消