Build and Use jieba-php: Chinese Word Segmentation in PHP via Rust
This guide explains how to install the jieba-php extension— a Rust‑based Chinese word segmentation library for PHP—by listing required dependencies, showing the cargo build steps, demonstrating runtime commands, describing the provided API, and offering a complete usage example.
Overview
jieba-php provides Chinese word segmentation for PHP by wrapping the Rust implementation of the Jieba library.
Dependencies
rust
php-dev
Build
# Optional, specify if php isn’t installed globally.
# export PHP_CONFIG=<Your path of php-config>
# Build libjieba.so.
cargo build --releaseRun
php -d "extension=target/release/libjieba.so" --ri jieba
php -d "extension=target/release/libjieba.so" -r "print_r((new Jieba())->cut('我们中出了一个叛徒'));"API
class Jieba {
public function __construct();
public function cut(string $sentence, bool $hmm): array;
public function cutForSearch(string $sentence, bool $hmm): array;
public function cutAll(string $sentence): array;
}Example
<?php
$jieba = new Jieba();
$words = $jieba->cut("我们中出了一个叛徒", true);
print_r($words);
$words = $jieba->cutAll("我们中出了一个叛徒");
print_r($words);
$words = $jieba->cutForSearch("我们中出了一个叛徒");
print_r($words);Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
