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

x
  • 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:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24147219.html

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')
<?php
	
// Reading file
$sFileName = 'utf8.txt';
$hFile = fopen($sFileName, 'rb');
$contents = fread($hFile, filesize($sFileName));
fclose($hFile);
 
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

0
hankknight
Asked:
hankknight
  • 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.  
0
 
Ray PaseurCommented:
Thanks!  Best regards, ~Ray
0

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