Solved

How to detect binary chars in a file using preg_match in PHP 5?

Posted on 2009-04-10
8
934 Views
Last Modified: 2012-05-06
What pattern could I use to detect whether a file contains non-printable chars using preg_match (or ereg)?

The logic in the conditional below could be reversed depending upon the easiest pattern.

Thanks in advance.
$fileBuffer = file_get_contents($filePath);	
$pattern = '/pattern??/';
$result = preg_match($pattern, $fileBuffer);
if (false === $result)
    return "binary file";
else
    return "text file";

Open in new window

0
Comment
Question by:DigitalDave1
  • 3
  • 3
  • 2
8 Comments
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 24120236
The is_binary http://us2.php.net/is_binary function might be of interest.
0
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 24120246
My mistake, sorry, is_binary is available starting with PHP 6.
0
 

Author Comment

by:DigitalDave1
ID: 24120249
Yes I saw is_binary(). But we are running PHP 5.x.

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 24120253
I use a "clean_string()" function to remove not only binary characters, but also unwanted characters.  The code snippet just tests for numbers, but you can add all the alpha and special characters to the REGEX.

So something like this...

$str = "12345";
if (!is_clean_numeric_string($str)) die("BAD NUMBER!");

HTH, ~Ray
function is_clean_numeric_string($string) // Q-N-D IS IT NUMERIC?
{ 
   $str = trim(ereg_replace(" +", " ", $string));
   $new = ereg_replace("[^0-9]", "?", $str);
	
   if ($new != $str) 
   {
      return FALSE; 
   } else {
      return ( $new ); 
   }
}

Open in new window

0
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 24120255
Try using is_string http://us2.php.net/manual/en/function.is-string.php on $fileBuffer:
$fileBuffer = file_get_contents($filePath);     
 
$result = is_string($fileBuffer);
 
if (false === $result)
    return "binary file";
else
    return "text file";

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24120266
For a more expanded view of things, the pattern [\x00-\x1f] matches all control characters including the NUL.
0
 

Author Comment

by:DigitalDave1
ID: 24126414
Worked out a preg_match pattern to test for the non-printing chars that exclude \n \r \t  etc.:

$pattern = '/[\x00-\x08\x0E-\x1F\x7F]/';

Thanks for the clues that led to this idea.


0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24126507
Thanks for the points -- it's a good question! ~Ray
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Sessions in WordPress 8 32
PHP and google maps 13 45
PHP PDO   How do I correctly remove record using DELETE FROM? 1 21
PHP Syntax Error 4 27
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

803 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