Below is an example of how to count the number of characters in a string:
$palavra ="coisa";
echo strlen($palavra); //retorna o número 5
However, I'm getting this word from a text file and strlen is not working, see:
$f = fopen("palavras.txt", "r");
echo fgets($f); // Até aqui funciona: ecoa "casa".
echo strlen($f); //A primeira palavra do arquivo de texto é "casa",
// mas o echo não ecoa 4.
I tried to do it this way too and it did not work:
$f = fopen("palavras.txt", "r");
$palavra = fgets($f);
echo strlen($palavra); // Está ecoando 6 que não corresponde aos 4 caracteres da
//palavra "casa".
NOTE: The file currently contains 3 words , each in a line. But I intend to put more words.
I started to do it in the form below, but it is still not returning 4 characters for the house, it is always returning 3 characters more than the word I put in the first line in the file:
$f = fopen("palavras.txt", "r");
$palavra = fgets($f);
echo strlen(trim($palavra));
ADDED ON 08/25/2014
As each word is an array of characters I've been trying to print on the screen to check if it printed something more than the four letters of the word "house", I discovered that the word house is in:echo $palavra[3];
echo $palavra[4];
echo $palavra[5];
echo $palavra[6];
What's in the 0, 1, and 2? I made a for print all and the top three positions appear on the screen as diamonds with a question mark
I have set both the html goal and the save time file to for utf-8.
I've tried utf8_decode and nothing.
I figured if I always took 3 characters out of the result it would solve my problem I went searching and found this satckoverflow question in English: link
One face does just that, but another also warns that discarding the BOM is not a good idea, because even if one hour the BOM is not set I would not be counting 3 characters of my word. I do not want to play tricks. I want to understand.
Look at my final code working:
//Nesse arquivo na primeira linha tenho somente a palavra "casa"
$f = fopen("palavras.txt","r");
$palavra = fgets($f);
$car= strlen(trim($palavra)) - 3;
echo $car;
//Com o código acima retorno o valor 4, sem o (-3) retorna 7.
Will Ma be suitable?
** RESOLVED! SAVING WITHOUT THE BLESSED "GOOD"! NO NOTEPAD ++ HAS TO GO IN
ENCODING
BECAUSE NO WINDOWS NOTEPAD DOES NOT HAVE THAT OPTION. **
Thank you all! The answer from @Jader is very useful and I'm sure I'll use it, but according to the question if someone else in the forum needs this information @bfavaretto put everything.