PHP code example of masakielastic / icu4x
1. Go to this page and download the library: Download masakielastic/icu4x library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
masakielastic / icu4x example snippets
// Basic usage
$iterator = icu4x_segmenter("Hello World");
foreach ($iterator as $segment) {
echo $segment . "\n";
}
// With parameters
$iterator = icu4x_segmenter("こんにちは👋世界", "grapheme", null);
echo "Total segments: " . count($iterator) . "\n";
// Complex Unicode text
$text = "🇺🇸🏳️🌈👨👩👧👦";
$segments = icu4x_segmenter($text);
foreach ($segments as $i => $segment) {
echo "[$i] => '$segment'\n";
}
// Create segmenter instance
$segmenter = new ICU4X\Segmenter('grapheme', null);
// Segment text
$iterator = $segmenter->segment("Hello World");
// Iterate over segments
foreach ($iterator as $segment) {
echo $segment . "\n";
}
// Use SPL interfaces
echo "Count: " . count($iterator) . "\n";
echo "Is countable: " . ($iterator instanceof Countable ? "Yes" : "No") . "\n";
echo "Is iterable: " . ($iterator instanceof IteratorAggregate ? "Yes" : "No") . "\n";
echo icu4x_eaw_width('A'); // 1 (Narrow)
echo icu4x_eaw_width('あ'); // 2 (Wide)
echo icu4x_eaw_width('ア'); // 1 (Halfwidth)
echo icu4x_eaw_width('A'); // 2 (Fullwidth)
echo icu4x_eaw_width('§'); // 1 (Ambiguous, default)
echo icu4x_eaw_width('§', 'ja'); // 2 (Ambiguous, Japanese locale)
new ICU4X\Segmenter(string $mode = 'grapheme', ?string $locale = null)
// English text
$text = "Hello, world!";
$segments = icu4x_segmenter($text);
// Output: ['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!']
// Japanese text with emoji
$text = "こんにちは👋世界";
$segments = icu4x_segmenter($text);
// Output: ['こ', 'ん', 'に', 'ち', 'は', '👋', '世', '界']
// Complex emoji sequences
$text = "👨👩👧👦";
$segments = icu4x_segmenter($text);
// Output: ['👨👩👧👦'] (single family emoji)
$iterator = icu4x_segmenter("Hello");
// Countable interface
echo count($iterator); // 5
// IteratorAggregate interface
foreach ($iterator as $index => $segment) {
echo "Position $index: $segment\n";
}
// Check interface implementation
var_dump($iterator instanceof Countable); // true
var_dump($iterator instanceof IteratorAggregate); // true
// Basic width calculation
$text = "Hello世界";
$width = 0;
for ($i = 0; $i < mb_strlen($text); $i++) {
$char = mb_substr($text, $i, 1);
$width += icu4x_eaw_width($char);
}
echo "Display width: $width\n"; // 9
// Locale-specific handling of ambiguous characters
$ambiguous = "§±×÷";
echo "Default: ";
for ($i = 0; $i < mb_strlen($ambiguous); $i++) {
$char = mb_substr($ambiguous, $i, 1);
echo icu4x_eaw_width($char);
}
echo "\n"; // 1111
echo "Japanese: ";
for ($i = 0; $i < mb_strlen($ambiguous); $i++) {
$char = mb_substr($ambiguous, $i, 1);
echo icu4x_eaw_width($char, 'ja');
}
echo "\n"; // 2222
ini
extension=icu4x
bash
git clone https://github.com/masakielastic/php-ext-icu4x.git
cd php-ext-icu4x
bash
echo "extension=icu4x" >> $(php-config --ini-path)/php.ini
bash
# Build in release mode
cargo build --release
# Run tests with release build
php -d extension=target/release/libicu4x.so tests/basic_test.php