简体   繁体   English

无法在 PHP 中将斜杠写入文件

[英]Can not write slash into file in PHP

I am trying to write simple string into file which should contain backslash, when I print on the screen backslash is there but when write into file using fwrite it is removed.我试图将简单的字符串写入应该包含反斜杠的文件中,当我在屏幕上打印时,反斜杠存在,但是当使用 fwrite 写入文件时,它被删除。 I have try double, quadruple escape it still doesn't work我已经尝试了双重、四重逃逸,它仍然不起作用

$key="Hello";
$trs ="This place is 'too cool'";
$removed = str_replace("'", '\\\\\'', $trs);
if($removed!=$trs){
     echo $removed."<br>";
}
fwrite($file, "'$key' => '".$removed."'" . ",\n");

When run the code I will get - This place is \\\\'too cool\\\\' But when the same value is written to the file I will get This place is 'too cool'运行代码时我会得到 - This place is \\\\'too cool\\\\'但是当相同的值写入文件时,我会得到This place is 'too cool'

如果需要反斜杠 \\ 作为字符并且不应是转义序列,则可以在双引号中使用 \\x5C。

$removed = str_replace("'", "\x5C\x5C'", $trs);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM