Byte order mark: Difference between revisions
Jump to navigation
Jump to search
m (→MySQL way) |
No edit summary |
||
| Line 45: | Line 45: | ||
</table> | </table> | ||
=== PHP way === | |||
PHP code<ref>[https://stackoverflow.com/questions/14674834/php-convert-string-to-hex-and-hex-to-string PHP convert string to hex and hex to string - Stack Overflow]</ref>: | |||
<pre> | |||
$string = "1234567890"; | |||
echo $string . " NOT contains BOM --> after str2hex: " . str2hex($string) . PHP_EOL; | |||
$string = "\xEF\xBB\xBF" . "1234567890"; | |||
echo $string . " contains BOM --> after str2hex: " . str2hex($string) . PHP_EOL; | |||
function str2hex($string) { | |||
$hexstr = unpack('H*', $string); | |||
return array_shift($hexstr); | |||
} | |||
</pre> | |||
Result: | |||
<pre> | |||
1234567890 NOT contains BOM --> after str2hex: 31323334353637383930 | |||
1234567890 contains BOM --> after str2hex: efbbbf31323334353637383930 | |||
</pre> | |||
=== File command way === | === File command way === | ||
Revision as of 13:47, 23 April 2018
Byte order mark (BOM, 位元組順序記號)
How to see Byte order mark
MySQL way
Using MySQL HEX() function "returns a string representation of a hexadecimal value of a decimal or string value specified as an argument."
Run sql on sqlfiddle.com or Download the Sql file directly.
CREATE TABLE `articles` (
`id` varchar(50) NOT NULL,
`notes` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `articles` (`id`, `notes`) VALUES
('1234567890', 'no BOM'),
('1234567890', 'BOM');
ALTER TABLE `articles`
ADD UNIQUE KEY `id` (`id`) USING BTREE;
SELECT HEX(id), id, notes FROM articles;
| HEX(id) | id | notes |
|---|---|---|
| 31323334353637383930 | 1234567890 | no BOM |
| EFBBBF31323334353637383930 | 1234567890 | BOM |
PHP way
PHP code[1]:
$string = "1234567890";
echo $string . " NOT contains BOM --> after str2hex: " . str2hex($string) . PHP_EOL;
$string = "\xEF\xBB\xBF" . "1234567890";
echo $string . " contains BOM --> after str2hex: " . str2hex($string) . PHP_EOL;
function str2hex($string) {
$hexstr = unpack('H*', $string);
return array_shift($hexstr);
}
Result:
1234567890 NOT contains BOM --> after str2hex: 31323334353637383930 1234567890 contains BOM --> after str2hex: efbbbf31323334353637383930
File command way
Using file (command): file filename.txt on Linux
, macOS
[2] & Cygwin on Win
| File content | Example result returned by file command |
|---|---|
| File contains BOM | UTF-8 Unicode (with BOM) text |
| File NOT contains BOM | UTF-8 Unicode text |