PHPackages                             ranvis/mecab - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ranvis/mecab

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ranvis/mecab
============

MeCab binding using FFI

v0.3.3(2y ago)5733.1k↓15%2BSD-2-ClausePHPPHP &gt;=8.0.0

Since Dec 16Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/ranvis/php-mecab-ffi)[ Packagist](https://packagist.org/packages/ranvis/mecab)[ Docs](https://github.com/ranvis/php-mecab-ffi/blob/master/README.md)[ RSS](/packages/ranvis-mecab/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (10)Used By (0)

PHP-MeCab-FFI
=============

[](#php-mecab-ffi)

MeCab binding using FFI.

- [Changelog](CHANGELOG.md)

License
-------

[](#license)

BSD 2-Clause License

This library contains a part of MeCab interface definitions, which is licensed under BSD 3-Clause License as stated there.

Installation
------------

[](#installation)

First make sure you have enabled FFI extension, which is bundled with PHP. Then execute the Composer command:

`composer require "ranvis/mecab:^0.3"`

Make sure you have installed [MeCab](http://taku910.github.io/mecab/) 0.996 (or later compatible version) on your system along with the dictionary. On Windows and some Linux distros, there should be a pre-built package.

Last but not least, if you are going to use the library from a non-CLI environment such as a web server, `ffi.enable=true` instead of the restricted default `ffi.enable=preload` must be set in the system INI configuration used by the SAPI.

Example Usage
-------------

[](#example-usage)

```
use Ranvis\MeCab;

require_once __DIR__ . '/vendor/autoload.php';

$mecab = new MeCab\Env(); // libmecab.{so,dll} should be in the PATH directory
//$mecab = new MeCab\Env('libmecab.so.2'); // or libmecab.so.2 in it
//$mecab = new MeCab\Env('/usr/lib64/libmecab.so.2'); // or specify explicitly
var_dump($mecab->getVersion());

$tagger = $mecab->tagger();
//$tagger = $mecab->tagger(['--rcfile', '/path/to/mecabrc']);

foreach ($tagger->getDictionaryInfo() as $info) {
    $name = $info->getFileName();
    $name = substr($name, strlen(dirname($name, 2)));
    printf("Dictionary: %s, Version: %d, Encoding: %s\n", $name, $info->getVersion(), $info->getCharset());
}

$headNode = $tagger->parseToNode("メカブはおやつに入りますか？");
foreach ($headNode as $node) {
    echo $node->surface() . ": " . $node->feature() . "\n";
}
```

Preloading Dynamic Library
--------------------------

[](#preloading-dynamic-library)

Using scripts and FFI to load libraries incurs a small overhead compared to native PHP extensions. This can be mitigated by using FFI's preload feature. With preloading, daemon-like SAPI such as FPM can preprocess initialization of the library and reuse it afterwards.

### Preload using `ffi.preload`

[](#preload-using-ffipreload)

To make use of this, we need to generate a header file for MeCab once. Run bundled `gen_mecab_preloader` command with the destination (and MeCab library name/path).

```
$ mkdir ffi_preload.d
$ vendor/bin/gen_mecab_preloader ffi_preload.d/mecab.h /path/to/libmecab.so
Generated: ffi_preload.d/mecab.h
```

Then set `ffi.preload` ini value to point to the file. (The header file may have to be regenerated in case this library is largely updated.)

Now to see if it works, we use CLI to run the following script. Notice that `MeCab\Env` is now instantiated with the `MeCab\Env::fromScope()` static method instead of a `new` operator, to take advantage of preloading.

```
$ cat  preload_test.php
