Php 如何替换\';在文件中?

Php 如何替换\';在文件中?,php,Php,如何使用php替换文本中的2个特殊字符? 我试过: $file_contents = file_get_contents($path_to_file); $file_contents = str_replace('Devo "cambiare" l\'Episodio', 'Con "questo" Episodio', $file_contents); file_put_contents($path_to_file,$file_contents); 但

如何使用php替换文本中的2个特殊字符? 我试过:

$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace('Devo "cambiare" l\'Episodio', 'Con "questo" Episodio', $file_contents);
file_put_contents($path_to_file,$file_contents);
但是它不起作用,因为它没有读到我的角色,也没有在文本中找到它。 我试过
\\'
,但不起作用。 如何使用php更改100个文件中的特殊字符串?

使用preg\u replace

syntex:preg_replace('要替换的特殊字符','替换为',输入字符串)

您的字符串位置不正确,请首先更正它,您使用的单逗号和双逗号令人困惑,请将其清除

您也可以使用str_replace

$file_contents = "abc \\\Episodio xyz";
$data = str_replace('\\\Episodio', 'Episodio', $file_contents);
使用strtr(string_original,array$replacements),这里使用文件解析的另一点是在解析大文件时考虑内存


参考资料:

什么是“不起作用”?您观察到了什么错误或其他意外行为?
“Devo”cambiare“l\\\\\\\'eposodio”
我的意思是它不起作用,因为它不能取代它们。如果我把\\\'它不替换itI不能复制为
\\\\'
.preg\u replace('\'epiodio',''epiodio',$file\u contents);或preg\u replace('\\\'epiodio',''epiodio',$file\u contents);不起作用。它说我警告:preg_replace():找不到结尾分隔符“”,或警告:preg_replace():分隔符不能是字母数字或反斜杠,带有此->$replacements=['\'''''''''=>'”];$file_contents=strtr($file_contents,$replacements);它不起作用,页面变为空这是一个您想要做的示例:(希望对您有所帮助)
// $replacements = ['from' => 'to', ...]
$file_contents = strtr($file_contents, $replacements);