1.时间处理
刚刚 一天前 一个月前
public function timeCode($param)
{
$now_time = time();
if ($now_time - $param < 60 * 3) {
$putTime = '刚刚';
} else if ($now_time - $param < 60 * 60) {
$putTime = ceil(($now_time - $param) / 60) . '分钟前';
} else if ($now_time - $param < 60 * 60 * 24) {
$putTime = ceil(($now_time - $param) / 60 / 60) . '小时前';
} else if ($now_time - $param < 60 * 60 * 24 * 30) {
$putTime = ceil(($now_time - $param) / 60 / 60 / 24) . '天前';
} else {
$putTime = ceil(($now_time - $param) / 60 / 60 / 24 / 30) . '月前';
//$putTime = date('Y-m-d h:i:s', $param);
}
return $putTime;
}
2.敏感词判断和替换
/**
*
* @param type $param
* @return type 敏感词替换
*/
public function FilterWordReplace($param = '')
{
$DisRedis = new \KIF\Cache\DisRedis();
$senWord = $DisRedis->get('senstive_word');
$word = preg_replace('/' . $senWord . '/i', "**", $param);
return $word;
}
/**
*
* @param type $pame
* @return string
* 敏感词判断
*/
public function doFilterWord($pame = '')
{
$DisRedis = new \KIF\Cache\DisRedis();
$senWord = $DisRedis->get('senstive_word');
$isWord = preg_match('/' . $senWord . '/i', $pame, $matchs);
if ($isWord) {
return $matchs;
} else {
return '';
}
}
3.php转换\n 为html
nl2br();
4.获取文章的摘要
/**
* 获取文章的摘要
*
* @param string $data
* @param int $cut 字数 自定义
* @param string $str
* @return type
*/
public function cutArticle($data, $cut = 200, $str = "...")
{
$data = strip_tags($data);
$pattern = "/&[a-zA-Z]+;/";
$data = preg_replace($pattern, '', $data);
if (!is_numeric($cut)) {
return $data;
}
if ($cut > 0) {
$data = trim(mb_strimwidth($data, 0, $cut, $str));
}
return $data;
}
5.获取联想词
public function helper()
{
$keyword = I('get.keyword', '', 'htmltotxt');
$data = file_get_contents("http://suggestion.baidu.com/su?wd={$keyword}&cb=");
// $data =ltrim($data, "(");
// $data =rtrim($data, ");");
$data = iconv('GB2312', 'UTF-8', $data);
//正则匹配所有中文
$list = preg_match_all("#(?:(?![,。?])[\xC0-\xFF][\x80-\xBF]+)+#", $data, $arr, PREG_PATTERN_ORDER);
$tmpArr = $arr['0'];
//去除重复
$uniqueArr = array_unique($tmpArr);
die(Json::minijson($uniqueArr));
}
网友评论