Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
wulijun committed Feb 13, 2019
2 parents bed44b8 + 8acc1ad commit 7324def
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 146 deletions.
88 changes: 49 additions & 39 deletions README.md
Expand Up @@ -7,60 +7,70 @@ php extension for spam word filter based on Double-Array Trie tree, it can detec

## 升级历史

### 2017-08-08
1. 同时支持php5&php7
1. 新增方法:
1. trie_filter_read,从string中读取二进制字典数据
1. trie_filter_write,将当前对象导出成二进制string
1. trie_filter_delete,从当前对象中删除一个word

### 2013-06-23
trie_filter_search_all,一次返回所有的命中词;修复内存泄露
1. trie_filter_search_all,一次返回所有的命中词
1. 修复内存泄露

## 依赖库

[libdatrie-0.2.4 or later](http://linux.thai.net/~thep/datrie/datrie.html)

## 安装步骤

下面的$LIB_PATH为依赖库安装目录,$INSTALL_PHP_PATH为PHP5安装目录
下面的$LIB_PATH为依赖库安装目录,$INSTALL_PHP_PATH为PHP安装目录

### 安装libdatrie
$ tar zxvf libdatrie-0.2.4.tar.gz
$ cd libdatrie-0.2.4
$ make clean
$ ./configure --prefix=$LIB_PATH
$ make
$ make install

### 安装扩展
$ $INSTALL_PHP_PATH/bin/phpize
$ ./configure --with-php-config=$INSTALL_PHP_PATH/bin/php-config --with-trie_filter=$LIB_PATH
$ make
$ make install

```
$ tar zxvf libdatrie-0.2.4.tar.gz
$ cd libdatrie-0.2.4
$ make clean
$ ./configure --prefix=$LIB_PATH
$ make
$ make install
```
### 安装扩展
```
$ $INSTALL_PHP_PATH/bin/phpize
$ ./configure --with-php-config=$INSTALL_PHP_PATH/bin/php-config --with-trie_filter=$LIB_PATH
$ make
$ make install
```
然后修改php.ini,增加一行:extension=trie_filter.so,然后重启PHP。

## 使用示例
<?php
$arrWord = array('word1', 'word2', 'word3');
$resTrie = trie_filter_new(); //create an empty trie tree
foreach ($arrWord as $k => $v) {
trie_filter_store($resTrie, $v);
}
trie_filter_save($resTrie, __DIR__ . '/blackword.tree');

$resTrie = trie_filter_load(__DIR__ . '/blackword.tree');

$strContent = 'hello word2 word1';
$arrRet = trie_filter_search($resTrie, $strContent);
print_r($arrRet); //Array(0 => 6, 1 => 5)
echo substr($strContent, $arrRet[0], $arrRet[1]); //word2
$arrRet = trie_filter_search_all($resTrie, $strContent);
print_r($arrRet); //Array(0 => Array(0 => 6, 1 => 5), 1 => Array(0 => 12, 1 => 5))

$arrRet = trie_filter_search($resTrie, 'hello word');
print_r($arrRet); //Array()

trie_filter_free($resTrie);

```
<?php
$arrWord = array('word1', 'word2', 'word3');
$resTrie = trie_filter_new(); //create an empty trie tree
foreach ($arrWord as $k => $v) {
trie_filter_store($resTrie, $v);
}
trie_filter_save($resTrie, __DIR__ . '/blackword.tree');
$resTrie = trie_filter_load(__DIR__ . '/blackword.tree');
$strContent = 'hello word2 word1';
$arrRet = trie_filter_search($resTrie, $strContent);
print_r($arrRet); //Array(0 => 6, 1 => 5)
echo substr($strContent, $arrRet[0], $arrRet[1]); //word2
$arrRet = trie_filter_search_all($resTrie, $strContent);
print_r($arrRet); //Array(0 => Array(0 => 6, 1 => 5), 1 => Array(0 => 12, 1 => 5))
$arrRet = trie_filter_search($resTrie, 'hello word');
print_r($arrRet); //Array()
trie_filter_free($resTrie);
```
# PHP版本

PHP 5.2 or later.
PHP 5.2 ~ 7.1.

Windows is not support until now.

Expand Down
24 changes: 24 additions & 0 deletions php7_wrapper.h
@@ -0,0 +1,24 @@
#ifndef EXT_PHP_TRIE_FILTER_PHP7_WRAPPER_H_
#define EXT_PHP_TRIE_FILTER_PHP7_WRAPPER_H_

#include "ext/standard/php_http.h"

#if PHP_MAJOR_VERSION < 7
typedef int zend_size_t;

#define TRIE_ZEND_REGISTER_RESOURCE ZEND_REGISTER_RESOURCE
#define TRIE_ZEND_FETCH_RESOURCE ZEND_FETCH_RESOURCE
#define TRIE_MAKE_STD_ZVAL(p) MAKE_STD_ZVAL(p)
#define TRIE_RESOURCE_FREE(resource) zend_list_delete(Z_RESVAL_P(resource))
#else /* PHP Version 7 */
typedef size_t zend_size_t;
typedef zend_resource zend_rsrc_list_entry;

#define TRIE_ZEND_REGISTER_RESOURCE(return_value, result, le_result) ZVAL_RES(return_value,zend_register_resource(result, le_result))
#define TRIE_ZEND_FETCH_RESOURCE(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type) \
(rsrc = (rsrc_type) zend_fetch_resource(Z_RES_P(*passed_id), resource_type_name, resource_type))
#define TRIE_MAKE_STD_ZVAL(p) zval _stack_zval_##p; p = &(_stack_zval_##p)
#define TRIE_RESOURCE_FREE(resource) zend_list_close(Z_RES_P(resource))
#endif /* PHP Version */

#endif /* EXT_PHP_TRIE_FILTER_PHP7_WRAPPER_H_ */
3 changes: 3 additions & 0 deletions php_trie_filter.h
Expand Up @@ -47,11 +47,14 @@ PHP_RSHUTDOWN_FUNCTION(trie_filter);
PHP_MINFO_FUNCTION(trie_filter);

PHP_FUNCTION(trie_filter_load);
PHP_FUNCTION(trie_filter_read);
PHP_FUNCTION(trie_filter_search);
PHP_FUNCTION(trie_filter_search_all);
PHP_FUNCTION(trie_filter_new);
PHP_FUNCTION(trie_filter_store);
PHP_FUNCTION(trie_filter_delete);
PHP_FUNCTION(trie_filter_save);
PHP_FUNCTION(trie_filter_write);
PHP_FUNCTION(trie_filter_free);

#ifdef ZTS
Expand Down

0 comments on commit 7324def

Please sign in to comment.