0

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.

Qhiliqq
  • 79
  • 7
  • It is better to use `fopen()` – Raptor Mar 01 '21 at 09:12
  • 1
    @Raptor ...because? Genuine enquiry here, especially in so far as it would relate to this case. – ADyson Mar 01 '21 at 09:12
  • Please read [this](https://stackoverflow.com/q/24007898/188331) – Raptor Mar 01 '21 at 09:20
  • Also, show us the content that is in test.htm – John Doe Mar 01 '21 at 09:20
  • @Raptor and why would such fine-grained control over the file-reading process be necessary in this case? Did I miss something? We're not dealing with raw binary data, or streaming here. – ADyson Mar 01 '21 at 23:23
  • Abusing `file_get_contents()` is a common mistake for PHP programmers. the function loads the whole file at the same time, which might allocate a lot of resources. – Raptor Mar 02 '21 at 01:45
  • @Raptor probably best to check the file size with the OP then before randomly handing out advice which might make their code complicated for no reason. As we can see in this case, it wasn't the issue. – ADyson Mar 02 '21 at 08:31

2 Answers2

2

Your code looks fine.

  1. Make sure you have actually 'Signature' in your code.
  2. Make sure you don't use any non-printable unicode characters with Signature.
  3. Append 'Signature' at the end of your test.htm and see if your code works.

Edited:

  1. Make sure your file use a valid and supported encoding( like UTF-8 )
Debuqer
  • 328
  • 2
  • 7
  • 1 and 2 is checked, as it is working with the whole file content as a variable. 3 doesn't work. I found out that if I create a new file and copy the whole content to that new file, the new file is 50% smaller and the replace work?!? – Qhiliqq Mar 01 '21 at 09:53
  • what is the encoding of your test.htm? utf8 worked fine to me. – Debuqer Mar 01 '21 at 10:13
  • Thats it @MB-abb ! The file is UCS-2 LE BOM – Qhiliqq Mar 01 '21 at 10:16
2

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!

Qhiliqq
  • 79
  • 7