Learn How to Work with IntlChar class in PHP7

0
2721
Learn-How-to-Work-with-IntlChar-class-in-PHP7-740X296

Learn-How-to-Work-with-IntlChar-class-in-PHP7-740X296
In the last chapter, we discussed about new special methods known as ‘closure call’ and ‘filtered unserialize’ with examples. In this chapter, we are going to discuss about IntlChar class that is added to PHP 7.

IntlChar class
IntlChar is a class that has been added to PHP 7. This class exposes additional ICU functionality. It has a number of constants and utility methods that can be used to access information and manipulate Unicode characters. These constants and methods resemble the names and behavior used by the underlying ICU library.

Setting up IntlChar Class
In order to use IntlChar class, we need to first install Intlextension to PHP 7. These are the following steps:

  • We’ve learnt how to install and set up XAMPP for the Apache web server and PHP 7 parser on the C drive. This was covered in chapter 1 of this tutorial.
  • Open the ‘C: \xampp\php\php.ini’ file in a text editor.
  • From this file, remove the semicolon (;) from ‘extension=php_intl.dll’ as shown below and save the file.
  • Restart the XAMPP web server.

Enabling PHP extension
Intlextension has been installed successfully and the IntlChar class is ready to use.

Table of Content for constants present in IntlChar class

S NoConstants present in IntlChar classImplementation in PHP 7 Program
1.const string UNICODE_VERSION = 6.3 ;IntlChar::UNICODE_VERSION
2.const integer CODEPOINT_MIN = 0 ;IntlChar::CODEPOINT_MIN
3.const integer CODEPOINT_MAX = 1114111 ;IntlChar::CODEPOINT_MAX
4.const integer PROPERTY_ALPHABETIC = 0 ;IntlChar::PROPERTY_ALPHABETIC
5.const integer PROPERTY_BINARY_START = 0 ;IntlChar::PROPERTY_BINARY_START
6.const integer PROPERTY_ASCII_HEX_DIGIT = 1 ;IntlChar::PROPERTY_ASCII_HEX_DIGIT
7.const integer PROPERTY_BIDI_CONTROL = 2 ;IntlChar::PROPERTY_BIDI_CONTROL
8.const integer PROPERTY_BIDI_MIRRORED = 3 ;IntlChar::PROPERTY_BIDI_MIRRORED
9.const integer PROPERTY_DASH = 4 ;IntlChar::PROPERTY_DASH
10.const integer PROPERTY_DEFAULT_IGNORABLE_CODE_POINT = 5 ;IntlChar::
PROPERTY_DEFAULT_IGNORABLE_CODE_POINT
11.const integer PROPERTY_DEPRECATED = 6 ;IntlChar::PROPERTY_DEPRECATED
12.const integer PROPERTY_DIACRITIC = 7 ;IntlChar::PROPERTY_DIACRITIC
13.const integer PROPERTY_EXTENDER = 8 ;IntlChar::PROPERTY_EXTENDER
14.const integer PROPERTY_FULL_COMPOSITION_EXCLUSION = 9 ;IntlChar::
PROPERTY_FULL_COMPOSITION_EXCLUSION
15.const integer PROPERTY_GRAPHEME_BASE = 10 ;IntlChar::PROPERTY_GRAPHEME_BASE
16.const integer PROPERTY_GRAPHEME_EXTEND = 11 ;IntlChar::PROPERTY_GRAPHEME_EXTEND
17.const integer PROPERTY_GRAPHEME_LINK = 12 ;IntlChar::PROPERTY_GRAPHEME_LINK
18.const integer PROPERTY_HEX_DIGIT = 13 ;IntlChar::PROPERTY_HEX_DIGIT
19.const integer PROPERTY_HYPHEN = 14 ;IntlChar::PROPERTY_HYPHEN
20.const integer PROPERTY_ID_CONTINUE = 15 ;IntlChar::PROPERTY_ID_CONTINUE

Like these constants, there are around 600 constants present in IntlChar class that could be directly implemented in any PHP 7 program after enabling its extension in php.ini file.

Table of Content for utility methods present in IntlChar class

S No.Constants present in IntlChar classImplementation in PHP 7 ProgramDescription
1public static array charAge ( mixed $codepoint )IntlChar::charAgeThis method gets the “age” of the code point
2public static int charDigitValue ( mixed $codepoint )IntlChar::charDigitValueThis method gets the decimal digit value of a decimal digit character
3public static int charDirection ( mixed $codepoint )IntlChar::charDirectionThis method gets bidirectional category value for a code point
4public static int charFromName ( string $characterName [, int $nameChoice = IntlChar::UNICODE_CHAR_NAME ] )IntlChar::charFromNameThis method finds Unicode character by name and return its code point value
5public static mixed charMirror ( mixed $codepoint )IntlChar::charMirrorThis method gets the “mirror-image” character for a code point
6public static string charName ( mixed $codepoint [, int $nameChoice = IntlChar::UNICODE_CHAR_NAME ] )IntlChar::charNameThis method retrieves the name of a Unicode character
7public static int charType ( mixed $codepoint )IntlChar::charTypeThis method gets the general category value for a code point
8public static string chr ( mixed $codepoint )IntlChar::chrThis method returns Unicode character by code point value
9public static int digit ( string $codepoint [, int $radix = 10 ] )IntlChar::digitThis method gets the decimal digit value of a code point for a given radix
10public static void enumCharNames ( mixed $start , mixed $limit , callable $callback [, int $nameChoice = IntlChar::UNICODE_CHAR_NAME ] )IntlChar::enumCharNamesThis method enumerates all assigned Unicode characters within a range
11public static void enumCharTypes ( callable $callback )IntlChar::enumCharTypesThis method enumerates all code points with their Unicode general categories
12public static mixed foldCase ( mixed $codepoint [, int $options = IntlChar::FOLD_CASE_DEFAULT ] )IntlChar::foldCaseThis method performs case folding on a code point
13public static int forDigit ( int $digit [, int $radix = 10 ] )IntlChar::forDigitThis method gets character representation for a given digit and radix
14public static mixed getBidiPairedBracket ( mixed $codepoint )IntlChar::getBidiPairedBracketThis method gets the paired bracket character for a code point
15public static int getBlockCode ( mixed $codepoint )IntlChar::getBlockCodeThis method gets the Unicode allocation block containing a code point
16public static int getCombiningClass ( mixed $codepoint )IntlChar::getCombiningClassThis method gets the combining class of a code point
17public static string getFC_NFKC_Closure ( mixed $codepoint )IntlChar::getFC_NFKC_ClosureThis method gets the FC_NFKC_Closure property for a code point
18public static int getIntPropertyMaxValue ( int $property )IntlChar::getIntPropertyMaxValueThis method gets the max value for a Unicode property
19public static int getIntPropertyMinValue ( int $property )IntlChar::getIntPropertyMinValueThis method gets the min value for a Unicode property
20public static int getIntPropertyValue ( mixed $codepoint , int $property )IntlChar::getIntPropertyValueThis method gets the value for a Unicode property for a code point
21public static float getNumericValue ( mixed $codepoint )IntlChar::getNumericValueThis method gets the numeric value for a Unicode code point
22public static int getPropertyEnum ( string $alias )IntlChar::getPropertyEnumThis method gets the property constant value for a given property name
23public static string getPropertyName ( int $property [, int $nameChoice = IntlChar::LONG_PROPERTY_NAME ] )IntlChar::getPropertyNameThis method gets the Unicode name for a property
24public static int getPropertyValueEnum ( int $property , string $name )IntlChar::getPropertyValueEnumThis method gets the property value for a given value name
25public static string getPropertyValueName ( int $property , int $value [, int $nameChoice = IntlChar::LONG_PROPERTY_NAME ] )IntlChar::getPropertyValueNameThis method gets the Unicode name for a property value
26public static array getUnicodeVersion ( void )IntlChar::getUnicodeVersionThis method gets the Unicode version
27public static bool hasBinaryProperty ( mixed $codepoint , int $property )IntlChar::hasBinaryPropertyThis method checks a binary Unicode property for a code point
28public static bool isalnum ( mixed $codepoint )IntlChar::isalnumThis method checks if code point is an alphanumeric character
29public static bool isalpha ( mixed $codepoint )IntlChar::isalphaThis method checks if the code point is a letter character
30public static bool isbase ( mixed $codepoint )IntlChar::isbaseThis method checks if the code point is a base character
31public static bool isblank ( mixed $codepoint )IntlChar::isblankThis method checks if the code point is a “blank” or “horizontal space” character
32public static bool iscntrl ( mixed $codepoint )IntlChar::iscntrlThis method checks if the code point is a control character
33public static bool isdefined ( mixed $codepoint )IntlChar::isdefinedThis method checks whether the code point is defined
34public static bool isdigit ( mixed $codepoint )IntlChar::isdigitThis method checks if the code point is a digit character
35public static bool isgraph ( mixed $codepoint )IntlChar::isgraphThis method checks if the code point is a graphic character
36public static bool isIDIgnorable ( mixed $codepoint )IntlChar::isIDIgnorableThis method checks if the code point is an ignorable character
37public static bool isIDPart ( mixed $codepoint )IntlChar::isIDPartThis method checks if the code point is permissible in an identifier
38public static bool isIDStart ( mixed $codepoint )IntlChar::isIDStartThis method checks if the code point is permissible as the first character in an identifier
39public static bool isISOControl ( mixed $codepoint )IntlChar::isISOControlThis method checks if the code point is an ISO control code
40public static bool isJavaIDPart ( mixed $codepoint )IntlChar::isJavaIDPartThis method checks if the code point is permissible in a Java identifier
41public static bool isJavaIDStart ( mixed $codepoint )IntlChar::isJavaIDStartThis method checks if the code point is permissible as the first character in a Java identifier
42public static bool isJavaSpaceChar ( mixed $codepoint )IntlChar::isJavaSpaceCharThis method checks if the code point is a space character according to Java
43public static bool islower ( mixed $codepoint )IntlChar::islowerThis method checks if the code point is a lowercase letter
44public static bool isMirrored ( mixed $codepoint )IntlChar::isMirroredThis method checks if the code point has the Bidi_Mirrored property
45public static bool isprint ( mixed $codepoint )IntlChar::isprintThis method checks if the code point is a printable character
46public static bool ispunct ( mixed $codepoint )IntlChar::ispunctThis method checks if the code point is punctuation character
47public static bool isspace ( mixed $codepoint )IntlChar::isspaceThis method checks if the code point is a space character
48public static bool istitle ( mixed $codepoint )IntlChar::istitleThis method checks if the code point is a Titlecase letter
49public static bool isUAlphabetic ( mixed $codepoint )IntlChar::isUAlphabeticThis method checks if the code point has the Alphabetic Unicode property
50public static bool isULowercase ( mixed $codepoint )IntlChar::isULowercaseThis method checks if the code point has the Lowercase Unicode property
51public static bool isupper ( mixed $codepoint )IntlChar::isupperThis method checks if the code point has the general category “Lu” (uppercase letter)
52public static bool isUUppercase ( mixed $codepoint )IntlChar::isUUppercaseThis method checks if the code point has the Uppercase Unicode property
53public static bool isUWhiteSpace ( mixed $codepoint )IntlChar::isUWhiteSpaceThis method checks if the code point has the White_Space Unicode property
54public static bool isWhitespace ( mixed $codepoint )IntlChar::isWhitespaceThis method checks if the code point is a whitespace character according to ICU
55public static bool isxdigit ( mixed $codepoint )IntlChar::isxdigitThis method checks if the code point is a hexadecimal digit
56public static int ord ( mixed $character )IntlChar::ordThis method returns the Unicode code point value of character
57public static mixed tolower ( mixed $codepoint )IntlChar::tolowerThis method makes the Unicode character lowercase
58public static mixed totitle ( mixed $codepoint )IntlChar::totitleThis method makes the Unicode character titlecase
59public static mixed toupper ( mixed $codepoint )IntlChar::toupperThis method makes the Unicode character uppercase

PHP 7 Code example on IntlChar class
In the example below, we are just implementing few constants and utility method of IntlChar class.
Program
Explanation of code

  • By using IntlChar::UNICODE_VERSION, we are printing the version number which is 8.
  • By using the utility method IntlChar::charName (‘&’), we are printing the name of the character, which is ‘AMPERSAND’ here.
  • By using the utility method IntlChar::ispunct (‘!’), we are printing the Boolean value. Here, it is 1 which means true.

Output
As explained above, following is the output.
Output
Source Code For this Article is here

Conclusion
In this chapter, we explored the ‘IntlChar’ class that is a part of PHP 7 as opposed to PHP 5.x. In the next chapter, we are going to discuss about ‘CSPRNG’ which is another new feature that has added to PHP7.

LEAVE A REPLY

Please enter your comment!
Please enter your name here