Re: test for ascii or binary string

From: Date: Sat, 30 Nov 2002 12:38:04 +0000
Subject: Re: test for ascii or binary string
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-126528@lists.php.net to get a copy of this message
Morgan Hughes wrote:
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 just wrote this, it seems to work. I've used mhash to generate a binary string. It will fail if:
a) The strings use obscure ASCII control characters other than LF, CR and Horizontal Tab. b) You're expecting strings with european charater's with umlouts and stuff like that. You can add these characters to the list of exclusions on line 16 of this code. Hope this helps!!! Beth Gore <?php function isbinary($input) { /* This simple function returns true if there's any non-standard Ascii characters */ $isbinary = 0; for($x=0;$x < strlen($input); $x++) { $c = substr($input,$x,1); if($c < chr(32) or $c > chr(127)) { if($c != chr(10) or $c != chr(13) or $c != chr(9)) /* add expected european extended characters */ { $isbinary = 1; } } } return $isbinary; } $binary = mhash(MHASH_MD5,"test"); $ascii = "Hello, this is a normal string;"; $strings = array(1 => $binary, 2=> $ascii); while(list($id, $string) = each($strings)){ if(isbinary($string)){ echo "string ".$id." verified as binary<br>"; }else{ echo "string ".$id." verified as ascii<br>"; } } ?> --

« previous php.general (#126528) next »