简体   繁体   中英

How can I replace \' in a file?

How can I replace the 2 special chars ' in a text using php? I have tried:

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

but it doesn't work because it doesn't read me the ' character and can't find it in the text. I have tried \\\' but it doesn't work. How can I change the special string in 100 files with php?

use preg_replace.

syntex: preg_replace('special characters want to replace', 'replace with', input string);

Read more about preg_replace

Your string placement is not correct, first correct it, Your use of single and double comma is confusing, make it clean.

You can also use str_replace.

$file_contents = "abc \\\Episodio xyz";
$data = str_replace('\\\Episodio', 'Episodio', $file_contents);
// $replacements = ['from' => 'to', ...]
$file_contents = strtr($file_contents, $replacements);

Use strtr(string_original, array $replacements) , another point here when using files parsing is thinking about memory when parsing a large file.

Reference: strtr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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