PHPackages                             tianhe1986/fatahocorasick - 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. tianhe1986/fatahocorasick

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

tianhe1986/fatahocorasick
=========================

Aho-Corasick algorithm in php

1.3.0(6y ago)3262MITPHPPHP ^7.0

Since Nov 5Pushed 6y ago1 watchersCompare

[ Source](https://github.com/tianhe1986/FatAhoCorasick)[ Packagist](https://packagist.org/packages/tianhe1986/fatahocorasick)[ Docs](https://github.com/tianhe1986/FatAhoCorasick)[ RSS](/packages/tianhe1986-fatahocorasick/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (1)Versions (7)Used By (0)

FatAhoCorasick
==============

[](#fatahocorasick)

A little PHP library implementing the [Aho–Corasick algorithm](https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm)
The original paper cound be found [here](https://www.uio.no/studier/emner/matnat/ifi/INF3800/v13/undervisningsmateriale/aho_corasick.pdf)

一个纯PHP实现的 [Aho-Corasick算法](https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm)
算法的原论文可以看[这里](https://www.uio.no/studier/emner/matnat/ifi/INF3800/v13/undervisningsmateriale/aho_corasick.pdf)
百度搜出来的AC算法的中文讲解就那么几篇，转载来转载去的，但我表示看不懂。
索性一怒之下看原始的论文，然后根据论文中的算法写了这个PHP实现。
改天我也写篇中文讲解，争取比那几篇写得更容易懂一些。

Requires
========

[](#requires)

PHP 7.0 or higher

Installation
============

[](#installation)

```
 composer require tianhe1986/fatahocorasick

```

and then in your code

```
require_once __DIR__ . '/vendor/autoload.php';
use FatAhoCorasick\FatAhoCorasick;
```

Usage
=====

[](#usage)

### Basic

[](#basic)

```
$ac = new FatAhoCorasick();

//add keyword, string or array
$ac->addKeyword(['art', 'cart']);
$ac->addKeyword('ted');

//compute info
$ac->compute(false);  //not using next array

//$ac->compute(true);  using next array

//search
$result = $ac->search('a carted mart lot one blue ted');
```

`$result` would be like follows:

```
(
    [0] => Array
        (
            [0] => cart
            [1] => 2
        )

    [1] => Array
        (
            [0] => art
            [1] => 3
        )

    [2] => Array
        (
            [0] => ted
            [1] => 5
        )

    [3] => Array
        (
            [0] => art
            [1] => 10
        )

    [4] => Array
        (
            [0] => ted
            [1] => 27
        )

)

```

For each item in `$result`, item\[0\] means the keyword found, item\[1\] means its start location.

### Separate compute and search

[](#separate-compute-and-search)

Without `next` array:

```
$ac = new FatAhoCorasick();

//add keyword, string or array
$ac->addKeyword(['art', 'cart']);
$ac->addKeyword('ted');

//compute info
$ac->compute(false);  //not using next array

//get output, goto and failure, then you can store them
$output = $ac->getOutput();
$goto = $ac->getGoto();
$failure = $ac->getFailure();

$nac = new FatAhoCorasick();
$result = $nac->searchByFailure('a carted mart lot one blue ted', $output, $goto, $failure);

```

With `next` array:

```
$ac = new FatAhoCorasick();

//add keyword, string or array
$ac->addKeyword(['art', 'cart']);
$ac->addKeyword('ted');

//compute info
$ac->compute(true);  //not using next array

//get output, and next, then you can store them
$output = $ac->getOutput();
$next = $ac->getNext();

$nac = new FatAhoCorasick();
$result = $nac->searchByNext('a carted mart lot one blue ted', $output, $next);

```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~96 days

Total

4

Last Release

2454d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/640ea2d81be289342282579fb6436637ee991681f076c2e3f0a710fa231da6fe?d=identicon)[tianhe1986](/maintainers/tianhe1986)

---

Top Contributors

[![tianhe1986](https://avatars.githubusercontent.com/u/19562319?v=4)](https://github.com/tianhe1986 "tianhe1986 (13 commits)")

---

Tags

phpahocorasick

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tianhe1986-fatahocorasick/health.svg)

```
[![Health](https://phpackages.com/badges/tianhe1986-fatahocorasick/health.svg)](https://phpackages.com/packages/tianhe1986-fatahocorasick)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
