This function is great for input validation. I frequently need to check that all characters in a string are 7-bit ASCII (and not null). This is the fastest function I have found yet:
<?php
function is7bit($string) {
// empty strings are 7-bit clean
if (!strlen($string)) {
return true;
}
// count_chars returns the characters in ascending octet order
$str = count_chars($str, 3);
// Check for null character
if (!ord($str[0])) {
return false;
}
// Check for 8-bit character
if (ord($str[strlen($str)-1]) & 128) {
return false;
}
return true;
}
?>