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>";
}

}

?>

--



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



Reply via email to