0

I've been searching for this question, but I think it haven't been asked yet.

I have got a problem with reading via fgetc() from my file. When I need to read the apostrophe, the program replace it with ???, so I am not able to add apostrophe to my array. Here's the code (I cut it - so there's no array adding):

$file = fopen("file.txt", "r");
$read_c;
while(!feof($file)) {
    while(ctype_space($read_c = fgetc($file))); 
    echo $read_c . " ";
}
fclose($file);

Now, when there's an apostrophe in the text in the file

’a’

I get in the terminal:

? ? ? a ? ? ?

The strange thing is, when I put in the code

echo $read_c

Instead of

echo $read_c . " "

The output is given correctly

’a’

Thank you all for you help.

SRewip
  • 107
  • 5
  • 1
    `’a’ !== 'a'`..... there's apostrophes and there's apostrophes – Mark Baker Feb 21 '15 at 17:36
  • Now I realized, but does it matter? In the text are the first ones you've written. E: I didn't write the apostrophes anywhere in the code. – SRewip Feb 21 '15 at 17:38
  • What matters is charset.... `'` is pretty much universal, so will generally display without any issues, no matter what charset your page is set at; but `’` is likely to require a charset or UTF-8 – Mark Baker Feb 21 '15 at 17:47
  • I've tried to change all the types of apostrophes in the .txt file I read from. The result is the same anyway - question marks. – SRewip Feb 21 '15 at 17:50
  • Don't try changing it, ensure that the page you're displaying it on is set to display UTF-8 – Mark Baker Feb 21 '15 at 17:52
  • I don't know if I know what do you mean, but there's set encoding UTF-8 in the terminal and file. – SRewip Feb 21 '15 at 17:56
  • You're writing this output to a web browser aren't you? Then set appropriate page headers for the html you're creating to ensure that it's using UTF-8, e.g. ` ` – Mark Baker Feb 21 '15 at 17:59
  • No, I am not. I am using the php interpret on my OSX. It has nothing common with web browser. – SRewip Feb 21 '15 at 18:00
  • If you're sending this to CLI output, then the answers to [this question](http://stackoverflow.com/questions/4606570/os-x-terminal-utf-8-issues) may help – Mark Baker Feb 21 '15 at 18:02
  • I've tried this program on our UNIX server as well. I don't think it's OSX problem. The same result. I am really tired of this. – SRewip Feb 21 '15 at 18:06
  • The problem is a charset problem, nothing to do with PHP's fgets(), so you need to work out what charset is being used for your file.txt – Mark Baker Feb 21 '15 at 18:27

0 Answers0