ctype_* 家族
ctype_alnum(mixed $text): bool — ctype_alpha(mixed $text): bool — ctype_lower(mixed $text): bool — ctype_upper(mixed $text): bool — ctype_space(mixed $text): bool — ctype_xdigit(mixed $text): bool — ctype_graph(mixed $text): bool — ctype_print(mixed $text): bool — ctype_punct(mixed $text): bool
Ctype 其余九件聚合:alnum/alpha/lower/upper/space/xdigit/graph/print/punct 全部为窄 ASCII 语义;对照各语言 ASCII 窄语义与 Unicode 宽语义两派(ctype_digit 已单条)。
C C23
#include <ctype.h>
#include <stdio.h>
#include <string.h>
static int all_chars(const char *s, int (*pred)(int)) {
for (size_t i = 0; s[i] != '\0'; i++) {
if (pred((unsigned char)s[i]) == 0) return 0; /* 强转:负值 char 是 UB */
}
return 1;
}
int main(void) {
printf("%d\n", all_chars("abc123", isalnum) != 0);
printf("%d\n", all_chars("abc", isalpha) != 0);
printf("%d\n", all_chars("ABC", isupper) != 0);
printf("%d\n", all_chars("abc", islower) != 0);
printf("%d\n", all_chars(" \t\n", isspace) != 0);
printf("%d\n", all_chars("0f9A", isxdigit) != 0);
printf("%d\n", all_chars("a,b", ispunct) != 0);
printf("%d\n", all_chars("a b", isgraph) != 0);
printf("%d\n", all_chars("a b", isprint) != 0);
return 0;
}
1 1 1 1 1 1 0 0 1
C++ C++26
#include <cctype>
#include <cstdio>
/* std::is* 与 PHP ctype_* 同为窄 ASCII 语义 */
template <typename F>
static bool allChars(const char *s, F pred) {
for (size_t i = 0; s[i] != '\0'; i++) {
if (pred(static_cast<unsigned char>(s[i])) == 0) return false;
}
return true;
}
int main() {
std::printf("%d\n", allChars("abc123", [](unsigned char c) { return std::isalnum(c) != 0; }));
std::printf("%d\n", allChars("abc", [](unsigned char c) { return std::isalpha(c) != 0; }));
std::printf("%d\n", allChars("ABC", [](unsigned char c) { return std::isupper(c) != 0; }));
std::printf("%d\n", allChars("abc", [](unsigned char c) { return std::islower(c) != 0; }));
std::printf("%d\n", allChars(" \t\n", [](unsigned char c) { return std::isspace(c) != 0; }));
std::printf("%d\n", allChars("0f9A", [](unsigned char c) { return std::isxdigit(c) != 0; }));
std::printf("%d\n", allChars("a,b", [](unsigned char c) { return std::ispunct(c) != 0; }));
std::printf("%d\n", allChars("a b", [](unsigned char c) { return std::isgraph(c) != 0; }));
std::printf("%d\n", allChars("a b", [](unsigned char c) { return std::isprint(c) != 0; }));
return 0;
}
1 1 1 1 1 1 0 0 1
C# v11.0.100
// char.IsLetterOrDigit 等是 Unicode 宽语义;ctype 窄语义用显式 ASCII 区间
// (.NET 7+ 有部分 IsAscii* 助手如 IsAsciiLetterOrDigit,但区间写法全族统一)
static bool All(string s, Func<char, bool> p) => s.All(p);
Console.WriteLine(All("abc123", c => c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'));
Console.WriteLine(All("abc", c => c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'));
Console.WriteLine(All("ABC", c => c >= 'A' && c <= 'Z'));
Console.WriteLine(All("abc", c => c >= 'a' && c <= 'z'));
Console.WriteLine(All(" \t\n", c => c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'));
Console.WriteLine(All("0f9A", c => c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'));
Console.WriteLine(All("a,b", c => c >= '!' && c <= '/' || c >= ':' && c <= '@' || c >= '[' && c <= '`' || c >= '{' && c <= '~'));
Console.WriteLine(All("a b", c => c >= '!' && c <= '~')); // 可打印且非空白
Console.WriteLine(All("a b", c => c >= ' ' && c <= '~')); // 可打印含空格
True True True True True True False False True
Go v1.27.4
package main
import "fmt"
// Go 的 unicode.Is* 是 Unicode 宽语义;ctype 窄语义手写 ASCII 区间判断
// (这就是 PHP Ctype 扩展的 C 实现原貌)
func alnum(c rune) bool { return c < 0x80 && (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') }
func alpha(c rune) bool { return c < 0x80 && (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') }
func upper(c rune) bool { return c >= 'A' && c <= 'Z' }
func lower(c rune) bool { return c >= 'a' && c <= 'z' }
func space(c rune) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' }
func xdigit(c rune) bool { return c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F' }
func punct(c rune) bool {
return c < 0x80 && (c >= '!' && c <= '/' || c >= ':' && c <= '@' || c >= '[' && c <= '`' || c >= '{' && c <= '~')
}
func graph(c rune) bool { return c >= '!' && c <= '~' }
func print(c rune) bool { return c >= ' ' && c <= '~' }
func all(s string, pred func(rune) bool) bool {
for _, c := range s {
if !pred(c) {
return false
}
}
return true
}
func main() {
fmt.Println(all("abc123", alnum))
fmt.Println(all("abc", alpha))
fmt.Println(all("ABC", upper))
fmt.Println(all("abc", lower))
fmt.Println(all(" \t\n", space))
fmt.Println(all("0f9A", xdigit))
fmt.Println(all("a,b", punct))
fmt.Println(all("a b", graph))
fmt.Println(all("a b", print))
}
true true true true true true false false true
Java v27.0.1
import java.util.function.IntPredicate;
public class Main {
// Character.isLetterOrDigit 等是 Unicode 宽语义;ctype 窄语义手写区间判断
static boolean all(String s, IntPredicate p) {
return s.chars().allMatch(p);
}
static boolean ascii(int c) { return c < 128; }
public static void main(String[] args) {
System.out.println(all("abc123", c -> ascii(c) && (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9')));
System.out.println(all("abc", c -> ascii(c) && (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')));
System.out.println(all("ABC", c -> c >= 'A' && c <= 'Z'));
System.out.println(all("abc", c -> c >= 'a' && c <= 'z'));
System.out.println(all(" \t\n", c -> c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'));
System.out.println(all("0f9A", c -> c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'));
System.out.println(all("a,b", c -> ascii(c) && (c >= '!' && c <= '/' || c >= ':' && c <= '@' || c >= '[' && c <= '`' || c >= '{' && c <= '~')));
System.out.println(all("a b", c -> c >= '!' && c <= '~'));
System.out.println(all("a b", c -> c >= ' ' && c <= '~'));
}
}
true true true true true true false false true
JavaScript v26.13.1
// 窄 ASCII 正则即 ctype 的直接对应物
const t = (re, s) => re.test(s);
console.log(t(/^[A-Za-z0-9]+$/, "abc123"));
console.log(t(/^[A-Za-z]+$/, "abc"));
console.log(t(/^[A-Z]+$/, "ABC"));
console.log(t(/^[a-z]+$/, "abc"));
console.log(t(/^[\t\n\f\r ]+$/, " \t\n"));
console.log(t(/^[0-9A-Fa-f]+$/, "0f9A"));
console.log(t(/^[!-/:-@[-`{-~]+$/, "a,b")); // 仅标点:'a' 不通过
console.log(t(/^[!-~]+$/, "a b")); // graph:空格不通过
console.log(t(/^[ -~]+$/, "a b")); // print:空格算可打印
true true true true true true false false true
PHP v8.6.0
// ctype_* 家族:每个字符都必须满足谓词(窄 ASCII 语义)
var_dump(ctype_alnum("abc123")); // 字母或数字
var_dump(ctype_alpha("abc")); // 仅字母
var_dump(ctype_upper("ABC")); // 仅大写
var_dump(ctype_lower("abc")); // 仅小写
var_dump(ctype_space(" \t\n")); // 空白字符
var_dump(ctype_xdigit("0f9A")); // 十六进制字符
var_dump(ctype_punct("a,b")); // 仅标点:'a' 不通过
var_dump(ctype_graph("a b")); // 可打印且非空白:' ' 不通过
var_dump(ctype_print("a b")); // 可打印(含空格)
bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(false) bool(false) bool(true)
Python v3.15.2
import re
# Python 的 str.is* 是 Unicode 宽语义(全角/汉字也算);ctype 的窄 ASCII 语义
# 需锚定 ASCII 正则——两派都演示
print("abc123".isalnum()) # 宽语义
print("abc".isalpha()) # 宽语义
print("ABC".isupper()) # 宽语义
print("abc".islower()) # 宽语义
print(" \t\n".isspace()) # 宽语义
print(all(c in "0123456789abcdefABCDEF" for c in "0f9A")) # str 无 isxdigit 对应物
print(bool(re.fullmatch(r"[!-/:-@\[-`{-~]+", "a,b"))) # 窄标点:无 str 方法
print(bool(re.fullmatch(r"[!-~]+", "a b"))) # 窄 graph:空格不通过
print(bool(re.fullmatch(r"[ -~]+", "a b"))) # 窄 print
True True True True True True False False True
Rust v1.99.1
fn all_chars(s: &str, pred: fn(char) -> bool) -> bool {
s.chars().all(pred)
}
fn main() {
// char::is_ascii_* 是 ctype_* 逐一对应的窄 ASCII 原生实现
println!("{}", all_chars("abc123", |c| c.is_ascii_alphanumeric()));
println!("{}", all_chars("abc", |c| c.is_ascii_alphabetic()));
println!("{}", all_chars("ABC", |c| c.is_ascii_uppercase()));
println!("{}", all_chars("abc", |c| c.is_ascii_lowercase()));
println!("{}", all_chars(" \t\n", |c| c.is_ascii_whitespace()));
println!("{}", all_chars("0f9A", |c| c.is_ascii_hexdigit()));
println!("{}", all_chars("a,b", |c| c.is_ascii_punctuation()));
println!("{}", all_chars("a b", |c| c.is_ascii_graphic())); // graphic 不含空格
println!("{}", all_chars("a b", |c| c.is_ascii_graphic() || c == ' '));
}
true true true true true true false false true
TypeScript v26.13.1
// 运行时同 JS 正则,加类型化助手
const t = (re: RegExp, s: string): boolean => re.test(s);
console.log(t(/^[A-Za-z0-9]+$/, "abc123"));
console.log(t(/^[A-Za-z]+$/, "abc"));
console.log(t(/^[A-Z]+$/, "ABC"));
console.log(t(/^[a-z]+$/, "abc"));
console.log(t(/^[\t\n\f\r ]+$/, " \t\n"));
console.log(t(/^[0-9A-Fa-f]+$/, "0f9A"));
console.log(t(/^[!-/:-@[-`{-~]+$/, "a,b"));
console.log(t(/^[!-~]+$/, "a b"));
console.log(t(/^[ -~]+$/, "a b"));
true true true true true true false false true
语言差异说明
C:ctype 家族的祖宗——isalnum/isalpha/isupper/islower/isspace/isxdigit/ispunct/isgraph/isprint;窄 ASCII 语义;返回「非零即真」须显式 != 0;参数必须能表示为 unsigned char(负值 char 是经典 UB,示例的强转为此而设)。
C++:std::is* 同 C(窄 ASCII + UB 陷阱 + 强转),C 代码可几乎原样迁移。
C#:默认 char.IsLetterOrDigit 等是 Unicode 宽语义;窄 ASCII 语义手写区间判断(.NET 7+ 提供 char.IsAsciiLetterOrDigit/IsAsciiHexDigit 等部分助手,但 upper/lower/space/punct 无对应物,示例统一用区间)。
Go:unicode.Is* 全是宽语义;ctype 窄语义手写 ASCII 区间判断——示例的 punct/graph/print 区间表就是 PHP Ctype 扩展的 C 实现原貌。
Java:Character.isLetterOrDigit 等是 Unicode 宽语义(Nd/Nl 全算);窄 ASCII 手写区间(示例),宽/窄两派差异与 PHP 对照最直观。
JavaScript:str.is* 不存在;窄 ASCII 用锚定正则(^[A-Za-z0-9]+$ 等),宽语义用 \p{...} + u 标志——宽窄开关在正则语法里。
PHP:Ctype 家族共 11 个函数(ctype_alnum/alpha/cntrl/digit/graph/lower/print/punct/space/upper/xdigit),语义统一为窄 ASCII(非 ASCII 字节一律 false);ctype_digit 已有单条,本条聚合其余。两派对照:PHP/C/C++/Rust 的 ASCII 系 vs Python/Java/Go/C# 默认的 Unicode 宽语义系(全角字母在宽语义下 alnum 为真)。
Python:str.is* 是 Unicode 宽语义;str 没有 isxdigit/isgraph/isprint 对应物(xdigit 用 string.hexdigits 成员判断,graph/print 用 ASCII 区间正则);要 PHP 的窄语义就锚定 ASCII 正则。
Rust:char::is_ascii_* 家族(is_ascii_alphanumeric/uppercase/lowercase/whitespace/hexdigit/punctuation/graphic)是与 PHP 逐一对应的窄 ASCII 原生实现;宽语义另用 char::is_*(Unicode)。
TypeScript:运行时同 JS 正则;类型标注不影响匹配语义。