G
G
gfan2014-01-11 07:28:19
PHP
gfan, 2014-01-11 07:28:19

PHP: strlen and first line from file

In the loop I read the words line by line, here is part of the code

$word = file("word.txt");

$word = str_replace("\r\n", "", $word);
echo "===".strlen($word);

The fact is that strlen does not correctly determine the number of characters of the first word in word.txt
There is a word consisting of 8 characters, but strlen writes 11. With the second word in the second line (and with all the others), everything is displayed correctly. What is in the txt file at the very beginning, some character before the first word? What is causing this problem? In notepad ++ I turn on the display of all characters, there is nothing at the beginning.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Grachev, 2014-01-11
@gfan

BOM sequence? What encoding is shown? Notepad++ allows you to control it quite precisely. Set "UTF-8 without BOM".
Code for cutting out a BOM from a string:

$bom = pack("CCC", 0xef, 0xbb, 0xbf);
if (0 === strncmp($str, $bom, 3)) {
    echo "BOM detected - file is UTF-8\n";
    $str = substr($str, 3);
}
(taken from here )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question