[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

Checking hex to verify UTF-8: is uppercase required?

LordOfPorts provided me with a way to verify if a file was saved in Notepad with UTF-8 encoding:

His example used strtoupper.  Is that required?  

Could I use this:
       if(substr(bin2hex($contents), 0, 6) === 'efbbbf')
instead of this?
       if(strtoupper(substr(bin2hex($contents), 0, 6)) === 'EFBBBF')
// Reading file
$sFileName = 'utf8.txt';
$hFile = fopen($sFileName, 'rb');
$contents = fread($hFile, filesize($sFileName));
if(strtoupper(substr(bin2hex($contents), 0, 6)) === 'EFBBBF')
	echo 'UTF-8 encoded file recognized.';
	else echo 'Not recognized as a UTF-8 encoded file.';

Open in new window

  • 2
1 Solution
Ray PaseurCommented:
In this context, strtoupper is like having a belt and suspenders.  It's safer that way.  Either the lower case or the uppercase is acceptable output from bin2hex.  
Ray PaseurCommented:
Thanks!  Best regards, ~Ray

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now