note 113979 added to function.file
| From: | Anonymous | Date: | Wed, 25 Dec 2013 04:56:14 +0000 |
| Subject: | note 113979 added to function.file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198021@lists.php.net to get a copy of this message | ||
file() seems to have a problem in handling
UTF-16 with or without BOM.
file() is likely to think "\n"=LF (0A) as a line-ending.
So, not only "000A" but also "010A, 020A,...,FE0A, FF0A,..."
are regarded as line-endings.
Moreover, file() causes a serious problem in UTF-16LE.
file() loses first "0A" (the first half of "0A00")!
And the next line begins with "00" (the rest of "0A00").
So lines after the first "0A" are totally different.
To avoid this phenomena,
eg. in case (php_script : UTF-8 , file : UTF-16 with line-ending "\r\n"),
<?php
mb_regex_encoding('UTF-16'); // mb_ereg_search_..() can work properly
$str = file_get_contents($file_path);
$pattern = mb_convert_encoding('[^\r]*\r\n', 'UTF-16', 'UTF-8');
mb_ereg_search_init($str, $pattern);
while ($res = mb_ereg_search_regs()) {
$file[] = $res[0];
}
?>
instead of
$file = file($file_path);
If line-ending is "\n",
$pattern = mb_convert_encoding('[^\n]*\n', 'UTF-16', 'UTF-8');
----
Server IP: 49.212.134.217
Probable Submitter: 101.141.141.233
----
Manual Page -- http://php.net/manual/en/function.file.php
Edit -- https://master.php.net/note/edit/113979
Del: integrated -- https://master.php.net/note/delete/113979/integrated
Del: useless -- https://master.php.net/note/delete/113979/useless
Del: bad code -- https://master.php.net/note/delete/113979/bad+code
Del: spam -- https://master.php.net/note/delete/113979/spam
Del: non-english -- https://master.php.net/note/delete/113979/non-english
Del: in docs -- https://master.php.net/note/delete/113979/in+docs
Del: other reasons-- https://master.php.net/note/delete/113979
Reject -- https://master.php.net/note/reject/113979
Search -- https://master.php.net/manage/user-notes.php