Skip to content

Commit 8aa8e06

Browse files
author
wanghui03
committed
使用命名空间
1 parent 2c28d6d commit 8aa8e06

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

Core/Base.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: wanghui03
5+
* Date: 2017/7/24
6+
* Time: 11:27
7+
*/
8+
header('Content-type: text/html; charset=utf-8');
9+
date_default_timezone_set('PRC');
10+
11+
class Base
12+
{
13+
private static $_instance;
14+
15+
public function __clone() {}
16+
public function __construct()
17+
{
18+
spl_autoload_register(array('Base', 'autoload'));
19+
}
20+
21+
public function autoload($class)
22+
{
23+
$className = str_replace('\\', '/', $class);
24+
require_once BASE . '/' . $className . '.php';
25+
}
26+
27+
public static function init()
28+
{
29+
if (! self::$_instance instanceof self) {
30+
self::$_instance = new self();
31+
}
32+
return self::$_instance;
33+
}
34+
}

Lib/SensitiveWordFilterHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function setHashMap($sensitiveWord)
5050
$nowMap = $tempMap;
5151
} else {
5252
// 设置标志位
53-
$newMap = new LHashMap();
53+
$newMap = new HashMap();
5454
$newMap->put('isEnd', '0');
5555
// 添加到集合
5656
$nowMap->put($keyChar, $newMap);

autoload.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: wanghui03
5+
* Date: 2017/7/24
6+
* Time: 11:27
7+
*/
8+
9+
define('BASE', __DIR__);
10+
11+
require_once 'Core/Base.php';
12+
13+
Base::init();

demo.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@
55
* Date: 17/6/28
66
* Time: 下午5:54
77
*/
8-
header('Content-type: text/html; charset=utf-8');
9-
date_default_timezone_set('PRC');
10-
11-
require_once 'Lib/SensitiveWordFilter.php';
128

139
$sTime = microtime(true);
1410

15-
//获取铭感词库
11+
require_once 'autoload.php';
12+
13+
// 获取铭感词库
1614
$wordPool = file_get_contents('keyWord.txt');
1715
$wordData = explode(',', $wordPool);
18-
//构建敏感词hashMap
19-
$sensitiveWordMap = LSensitiveWordFilter::init()->setHashMap($wordData);
16+
17+
$sensitiveWordHelper = Lib\SensitiveWordFilterHelper::init();
18+
// 构建敏感词hashMap
19+
$sensitiveWordMap = $sensitiveWordHelper->setHashMap($wordData);
20+
21+
// 过滤
2022
$content = $_POST['content'];
21-
$filterContent = LSensitiveWordFilter::init()->replaceSensitiveWord($sensitiveWordMap, $content, '***');
23+
$filterContent = $sensitiveWordHelper->replaceSensitiveWord($sensitiveWordMap, $content, '***');
2224

2325
$eTime = microtime(true);
24-
2526
//结果
2627
echo '<hr/>';
2728
echo '<hr/>';

0 commit comments

Comments
 (0)