3

Let's say I want to know the encoding of a text file, does PHP file_get_contents automatically turn contents into ASCII?

<pre>
<?php

$file = file_get_contents('./test.txt', true);

echo $file."<br>".mb_detect_encoding($file);
echo "<br>";
$file = file_get_contents('./test (2).txt', true); 

echo $file."<br>".mb_detect_encoding($file);
echo "<br>";
$file = file_get_contents('./test (3).txt', true);

echo $file."<br>".mb_detect_encoding($file);


?>
2
  • 8
    Nope, PHP doesn't care - it has no need to. So however the text was encoded to begin with will be what you're working with. Either that or it has been modified for transport - (mind reading mode)... are you by any chance using an FTP client in ASCII rather than binary mode?
    – Emissary
    Oct 21, 2014 at 7:51
  • @Emissary Heh... one of the many reasons my old projects all use ASCII - anything outside of that is encoded as an HTML entity before being sent to the server (and any non-ASCII input is rejected). Ah, how stupid I was to think that sweeping the problem under the carpet was a good idea! Oct 21, 2014 at 8:19

1 Answer 1

0

It does not convert the contents to ASCII. Because it is only getting the contents of a file it has no need to. More information is at https://www.php.net/manual/en/function.file-get-contents.php

1
  • While links are great, the resources can be moved or deleted. It is a good idea to provide explanations in the post in addition of the links.
    – mins
    Mar 19, 2015 at 17:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.