PHP code example of abelzhou / php-trie-tree
1. Go to this page and download the library: Download abelzhou/php-trie-tree 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/ */
abelzhou / php-trie-tree example snippets
stArr = array("张三","张四","王五","张大宝","张三四","张氏家族","王二麻子");
$tree = new \AbelZhou\Tree\TrieTree();
foreach ($testArr as $str){
$tree->append($str);
}
$res = $tree->getTree();
var_dump($res);
$res = $tree->search("有一个叫张三的哥们");
var_dump($res);
$res = $tree->search("我叫李四喜");
var_dump($res);
//删除
$res = $tree->delete("张三");
//删除整棵树 连带“张三”和张三下的“张三四”一并删除
$tree->delete("张三",true);
//拼音检测
$tree->append("zhangsan","",true,"张三");
$tree->append("zhangsan","",true,"张伞");
$t1 = microtime(true);
var_dump($tree->getTreeWord("zh"));
$t2 = microtime(true);
echo 'getTreeWordPinyin{' . ($t2 - $t1) . '}s'.PHP_EOL;
//replace & delete
$tree->append("z","",true,"在");
$tree->append("z","",true,"走");
$tree->append("z","",true,"做");
var_dump($tree->getTreeWord("z",4));
//覆盖
$tree->append("z",array("1"=>1),true,"做");
var_dump($tree->getTreeWord("z",4));
//删除
$tree->delete("z",false,true,"在");
var_dump($tree->getTreeWord("z",4));
$tree->delete("z",false,true,"走");
$tree->delete("z",false,true,"做");
var_dump($tree->getTreeWord("z", 4));
composer