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 charDirection() function in PHP
The IntlChar charDirection() function is used to determine the bidirectional category value for a given character, which is essential for proper text rendering in multilingual applications.
Syntax
int IntlChar::charDirection(mixed $codepoint)
Parameters
$codepoint − An integer codepoint value or a character encoded as a UTF-8 string.
Return Value
The function returns an integer representing the bidirectional category. Common return values include ?
IntlChar::CHAR_DIRECTION_LEFT_TO_RIGHT(0) − Left-to-right characters like Latin lettersIntlChar::CHAR_DIRECTION_RIGHT_TO_LEFT(1) − Right-to-left characters like Arabic lettersIntlChar::CHAR_DIRECTION_EUROPEAN_NUMBER(2) − European digits (0-9)IntlChar::CHAR_DIRECTION_EUROPEAN_NUMBER_SEPARATOR(3) − Separators like plus/minus signsIntlChar::CHAR_DIRECTION_OTHER_NEUTRAL(10) − Other neutral charactersAnd 18 other directional constants for various Unicode categories
Example
Here's how to check the directional properties of different characters ?
<?php
echo "Character direction analysis:<br>";
// Latin letter (Left-to-right)
echo "Letter 'A': " . IntlChar::charDirection("A") . "<br>";
// European number separator
echo "Plus sign '+': " . IntlChar::charDirection("+") . "<br>";
// Dash/minus sign
echo "Dash '-': " . IntlChar::charDirection("-") . "<br>";
// Other neutral character
echo "Asterisk '*': " . IntlChar::charDirection("*") . "<br>";
// Invalid multi-character string returns NULL
echo "Multi-char 'abc': ";
var_dump(IntlChar::charDirection("abc"));
// Using Unicode codepoint directly
echo "Unicode U+0041 (A): " . IntlChar::charDirection(0x0041) . "<br>";
?>
Character direction analysis: Letter 'A': 0 Plus sign '+': 3 Dash '-': 3 Asterisk '*': 10 Multi-char 'abc': NULL Unicode U+0041 (A): 0
Key Points
Returns
NULLfor invalid input or multi-character stringsAccepts both character strings and Unicode codepoint integers
Essential for implementing bidirectional text algorithms
Different character types return different directional category values
Conclusion
The IntlChar::charDirection() function is vital for internationalization, helping determine how characters should be rendered in mixed-direction text layouts. It returns specific integer constants representing Unicode bidirectional categories.
