PHP code example of overtrue / double-array-trie

1. Go to this page and download the library: Download overtrue/double-array-trie 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/ */

    

overtrue / double-array-trie example snippets


use Overtrue\DoubleArrayTrie\Builder;

$builder = new Builder();

$trie = $builder->build(['foo', 'bar', 'baz']);

$trie->export()->toFile('trie.json');
$trie->export()->toFile('trie.php');
$trie->export()->toFile('trie.dat');

use Overtrue\DoubleArrayTrie\Builder;

$builder = new Builder();

$trie = $builder->build([ 
            '一举' => 'yi ju',
            '一举一动' => 'yi ju yi dong',
        ]);

use Overtrue\DoubleArrayTrie\Factory;

$trie = Factory::loadFromFile('trie.json');
$trie = Factory::loadFromFile('trie.php');
$trie = Factory::loadFromFile('trie.dat');

use Overtrue\DoubleArrayTrie\Matcher;

$trie = Factory::loadFromFile('trie.json');
$matcher = new Matcher($trie);

// ['foo', 'bar', 'baz']

$matcher->match('foo'); // true
$matcher->match('oo'); // false

// ['一举' => 'yi ju', '一举一动' => 'yi ju yi dong']

$matcher->match('一举'); // 'yi ju'
$matcher->match('一举一'); // false

// ['一举' => 'yi ju', '一举一动' => 'yi ju yi dong', '一举成名' => 'yi ju cheng ming',]
$matcher->prefixMatch('一举一动都很奇怪'); 
// [
//  '一举' => 'yi ju',
//  '一举一动' => 'yi ju yi dong'
//]