Core\Validate::isAscii
Core\Validate::isAscii(string $s): boolCore\Validate::isAscii( string $s, ): bool
Checks whether every character of a text is an ASCII character. ASCII has 128 characters: the English letters without accents, the digits, common punctuation such as . and -, the space and the control characters. The method returns true or false.
Some systems accept only ASCII, for example a short code for a product, a file name on an old server, or a field in a network protocol. Core\Validate::isAscii checks the text before you send it there. A letter with an accent such as é, a Chinese character or an emoji is not ASCII, so the result is false. The empty text contains no other character, so the result is true.
isAscii does not check whether a character is visible. A tab or a newline is ASCII. Use Core\Validate::isPrintable to find those.
The examples below check a few words, count the texts in a list that need more than ASCII, and check a product code before it is saved.
Parameters
$sstringThe text to test.
Return value
Returns bool. true when every character is ASCII, and true for the empty string. false when the text contains any other character, such as é or 日.
Examples
Check a few words
<?nvs// Shows which texts contain only ASCII characters.
// "café" and "東京" are not ASCII. A tab is ASCII, and so is the empty text.array<string> $words = ['hello', 'Order #1042', 'café', '東京', "tab\there", ''];
// `Core\Validate::isAscii` returns `true` when every character is ASCII.// This prints yes, yes, no, no, yes and yes.foreach ($words as string $word) { echo Core\Validate::isAscii($word) ? 'yes' : 'no', "\n";}yesyesnonoyesyesOne byte for each character
<?nvs// Shows that an ASCII text uses exactly one byte for each character.
// "ë" and "ü" are not ASCII. Each of them needs two bytes.array<string> $names = ['Anna', 'Zoë', 'Jürgen Müller', 'Li Wei'];
// `Core\Str::length` counts characters and `Core\Bytes::length` counts bytes.// For "Anna" and "Li Wei", the two numbers are the same.foreach ($names as string $name) { string $kind = Core\Validate::isAscii($name) ? 'ASCII' : 'not ASCII'; echo $name, ' ', Core\Str::length($name), ' ', Core\Bytes::length($name as bytes), ' ', $kind, "\n";}Anna 4 4 ASCIIZoë 3 4 not ASCIIJürgen Müller 13 15 not ASCIILi Wei 6 6 ASCIICheck a product code
<?nvs// Shows how to check a product code from a form before it is saved.
class ProductCode{ // The warehouse system accepts only ASCII codes of 3 to 12 characters. // This function returns an error message, or an empty text when the code is fine. public static function problem(string $code): string { if (!Core\Validate::isAscii($code)) { return 'use only the letters A to Z, digits and simple punctuation'; } uint $length = Core\Str::length($code); if ($length < 3 || $length > 12) { return 'use 3 to 12 characters'; } return ''; }}
// Four codes as a user typed them. "ØL-7" contains "Ø", which is not ASCII.array<string> $codes = ['SKU-1042', 'ØL-7', 'AB', 'TSHIRT-RED-XL'];
// Only "SKU-1042" is saved. The other three codes each print their problem.foreach ($codes as string $code) { string $problem = ProductCode::problem($code); echo $code, ': ', $problem == '' ? 'saved' : $problem, "\n";}SKU-1042: savedØL-7: use only the letters A to Z, digits and simple punctuationAB: use 3 to 12 charactersTSHIRT-RED-XL: use 3 to 12 characters