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";
    print "mostly binary, use base64_encode";

   Morgan Hughes
   C programmer and highly caffeinated mammal.
   ICQ: 79293356

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to