Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
IntlChar charType() function in PHP
The IntlChar::charType() function determines the general Unicode category of a character. This function is part of PHP's Intl extension and helps classify characters according to Unicode standards.
Syntax
int IntlChar::charType(mixed $codepoint)
Parameters
$codepoint − An integer codepoint value or a character/string encoded as UTF-8.
Return Value
Returns an integer representing the general category. Common categories include −
IntlChar::CHAR_CATEGORY_UPPERCASE_LETTER− Uppercase letters (A-Z)IntlChar::CHAR_CATEGORY_LOWERCASE_LETTER− Lowercase letters (a-z)IntlChar::CHAR_CATEGORY_DECIMAL_DIGIT_NUMBER− Decimal digits (0-9)IntlChar::CHAR_CATEGORY_OTHER_PUNCTUATION− Punctuation marksIntlChar::CHAR_CATEGORY_MATH_SYMBOL− Mathematical symbolsIntlChar::CHAR_CATEGORY_CURRENCY_SYMBOL− Currency symbolsIntlChar::CHAR_CATEGORY_SPACE_SEPARATOR− Space characters
Example
Here's how to check character categories using charType() −
<?php
// Check uppercase letter
var_dump(IntlChar::charType("Z") === IntlChar::CHAR_CATEGORY_UPPERCASE_LETTER);
// Check lowercase letter
var_dump(IntlChar::charType("a") === IntlChar::CHAR_CATEGORY_LOWERCASE_LETTER);
// Check digit
var_dump(IntlChar::charType("5") === IntlChar::CHAR_CATEGORY_DECIMAL_DIGIT_NUMBER);
// Check punctuation
var_dump(IntlChar::charType(".") === IntlChar::CHAR_CATEGORY_OTHER_PUNCTUATION);
// Check space
var_dump(IntlChar::charType(" ") === IntlChar::CHAR_CATEGORY_SPACE_SEPARATOR);
// Get actual category value
echo "Category of 'A': " . IntlChar::charType("A") . "<br>";
echo "Category of '7': " . IntlChar::charType("7") . "<br>";
?>
bool(true) bool(true) bool(true) bool(true) bool(true) Category of 'A': 1 Category of '7': 9
Practical Usage
This function is useful for text processing and validation −
<?php
function analyzeText($text) {
$categories = [];
for ($i = 0; $i < mb_strlen($text); $i++) {
$char = mb_substr($text, $i, 1);
$type = IntlChar::charType($char);
if ($type === IntlChar::CHAR_CATEGORY_UPPERCASE_LETTER) {
$categories['uppercase']++;
} elseif ($type === IntlChar::CHAR_CATEGORY_LOWERCASE_LETTER) {
$categories['lowercase']++;
} elseif ($type === IntlChar::CHAR_CATEGORY_DECIMAL_DIGIT_NUMBER) {
$categories['digits']++;
}
}
return $categories;
}
$result = analyzeText("Hello123");
print_r($result);
?>
Array
(
[uppercase] => 1
[lowercase] => 4
[digits] => 3
)
Conclusion
The IntlChar::charType() function provides precise Unicode character classification, making it essential for internationalization and text processing tasks that require character type validation.
