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

Posted on 2009-02-23
Last Modified: 2012-05-06
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

Question by:hankknight
    LVL 107

    Accepted Solution

    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.  
    LVL 107

    Expert Comment

    by:Ray Paseur
    Thanks!  Best regards, ~Ray

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now