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 marks

  • IntlChar::CHAR_CATEGORY_MATH_SYMBOL − Mathematical symbols

  • IntlChar::CHAR_CATEGORY_CURRENCY_SYMBOL − Currency symbols

  • IntlChar::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.

Updated on: 2026-03-15T07:49:42+05:30

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements