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.

7
  • It is better to use fopen() Commented Mar 1, 2021 at 9:12
  • 1
    @Raptor ...because? Genuine enquiry here, especially in so far as it would relate to this case. Commented Mar 1, 2021 at 9:12
  • Please read this Commented Mar 1, 2021 at 9:20
  • Also, show us the content that is in test.htm Commented Mar 1, 2021 at 9: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. Commented Mar 1, 2021 at 23:23

2 Answers 2

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 )
Sign up to request clarification or add additional context in comments.

3 Comments

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?!?
what is the encoding of your test.htm? utf8 worked fine to me.
Thats it @MB-abb ! The file is UCS-2 LE BOM
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.