Reputation: 83
I'm trying to replace a string in a file, so far so good. The file is a htm file in html format bt this shouldn't be a problem.
My script looks like this:
$file = './test.htm';
$content = file_get_contents($file);
$str = str_replace('Signature','Test',$content);
file_put_contents('./test2.htm', $str);
The problem is str_replace doesn't replace the string "Signature", the output file has exactly the same content as my input file.
If I use the file content without file_get_contetnts, just while defining the string as a variable, my script works like a charme.
Upvotes: 0
Views: 325
Reputation: 83
With help from @MB-abb I found out that the encoding is UCS-2 LE BOM.
The added line in my script is now:
$str = mb_convert_encoding($str, "UTF-8", "UCS-2LE");
Which changes UCS-2LE to UTF-8. Now str_replace works like a charme.
Thanks!
Upvotes: 2
Reputation: 395
Your code looks fine.
Edited:
Upvotes: 2