8
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 am taking this word from a text file and the strlen it’s 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’ve tried to do it this way too and it didn’t 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".
OBS.: currently the file contains 3 words, each one in a row. But I intend to put more words.
I started to do the form below, but still it is not returning 4 characters to home, is always returning 3 characters more than apalavra that I put in the first line in the file:
$f = fopen("palavras.txt", "r");
$palavra = fgets($f);
echo strlen(trim($palavra));
ADDED ON 25/08/2014
Guys, since each word is an array of characters I was trying to print on the screen to check if it would print something more than the four letters of the word "home", I found that the word home is on:
echo $palavra[3];
echo $palavra[4];
echo $palavra[5];
echo $palavra[6];
What’s in 0, 1 and 2? I did a for printing all and the first three positions appear on the screen as lozenges with a question mark
I set both the html meta and the file when saving to utf-8 .
I’ve tried utf8_decode and nothing.
I figured if I always took out 3 characters of the result I would solve my problem I went to search and found this satckoverflow question in English: https://stackoverflow.com/questions/4057742/how-to-remove-efbbbf-in-php-string
One guy does exactly this, but another also warns that discarding the GOOD is not a good idea,even because if one time the GOOD is not set I would be failing to count 3 characters of my word. I don’t want to gambiarra. 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.
Ma will be fine?
**SOLVED! SAVING WITHOUT THE BLESSED "GOOD"! ON THE NOTEPAD++ HAS TO GO ON
ENCODING
BECAUSE WINDOWS NOTEPAD DOESN’T HAVE THAT OPTION.**
Thank you all! The @Jader reply is very useful and I will definitely use it, but according to the question if anyone else in the forum needs this information @bfavaretto put it all.
Does the file contain a single word? Always? Can it be two words? Several lines? . . The best is [Edit] the question to clarify this.
– brasofilo
Added details.
– I Wanna Know
Actually, I’m trying to understand your question and the reply of bfavaretto in the context of your comment "And how to measure only the word?"... if you only have one word per line and his code reads the first line, I don’t understand what the problem is...
– brasofilo
I know that logically speaking may have sounded strange to you, but is that even using the "Trim", keeps returning 3 more characters than the word actually has.
– I Wanna Know
Does this only happen with the first line? Does the other lines count? It may be the BOM of UTF-8 http://en.wikipedia.org/wiki/Byte_order_mark
– Marcos
Look, I’m going to use @Jader’s answer to check the other lines, but if I treat it that simple I don’t even know how to get the second line.
– I Wanna Know
The @bfavaretto mentioned this, but how do I remove GOOD if it is being employed? I set both the html meta and the file when saving to utf-8 .
– I Wanna Know