Re: test for ascii or binary string

From: Date: Thu, 01 Jan 1970 00:00:00 +0000
Subject: Re: test for ascii or binary string
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-126515@lists.php.net to get a copy of this message
On Fri, 29 Nov 2002, Jonathan Sharp wrote: > Is there a way to determine if a string has ascii or binary data in it? I've used this kind of approach in the past to determine which encoding to use on a string which may contain text or an image. Basically it uses addcslashes to escape non-ASCII chars, then strips all non-backslash chars, and takes the size of the string... $size should contain the number of bytes (out of 1024) that were escaped, and should be very low for clean text, and pretty high for binary. This was made for analyzing blobs, but maybe the approach is useful... $text = addcslashes(substr($string, 0, 1024), "\\\"'\0..\37\177..\377"); $size = strlen(preg_replace('/[^\\\\]/', '', $text)); if ($size < 200) print "mostly text, use addslashes"; else print "mostly binary, use base64_encode"; -- Morgan Hughes C programmer and highly caffeinated mammal. kyhm@kyhm.com ICQ: 79293356

« previous php.general (#126515) next »