$v) {
$result->{$k} = $v;
}
return $result;
}
/**
* 去除首尾全角及半角空格,多个空格合并为一个
* @param type $str
* @return string
*/
public static function trimAndMerge($str)
{
$str = preg_replace('/( | )+/', ' ', $str);
return trim(preg_replace("/^ +| +$/ ", " ", $str));
}
/**
* 去除首尾空格,包含全角空格
*/
public static function trim($str)
{
//$str = trim($str," \t\n\r\0\x0B");
$str = mb_ereg_replace('(^( | )+|( | )+$)', '', $str);
return $str;
}
/**
* 转换为友好时间
* @param int|string $timestamp
* @return string
*/
public static function convertToFriendlyTime($timestamp)
{
if (!is_numeric($timestamp)) {
$timestamp = strtotime($timestamp);
}
$now = time();
$interval = abs($now - $timestamp);
$today = strtotime(date("Ymd"));
$tomorrow = strtotime(date("Ymd", $now + 86400));
$thisYear = strtotime(date("Y-01-01 00:00"));
if ($interval < 60) {
if ($interval < 10) {
$interval = 10;
}
$string = $interval . "秒前";
} else if ($interval < 3600) {
$string = floor($interval / 60) . '分钟前';
} else if ($today < $timestamp AND $timestamp < $tomorrow) {
$string = '今天' . date('H:i', $timestamp);
} else if ($thisYear < $timestamp) {
$string = date('n月j日', $timestamp);
} else {
$string = date('Y-m-d', $timestamp);
}
return $string;
}
/**
* 写入ini文件
* @param type $path
* @param type $data
* @return type
*/
public static function writeIniFile($path, $data)
{
$content = null;
foreach ($data as $key => $item) {
if (is_array($item)) {
$content .= "\n[{$key}]\n";
foreach ($item as $k => $v) {
if (is_numeric($v) || is_bool($v)) {
$v = '"' . $v . '"';
}
$content .= "{$k} = {$v}\n";
}
} else {
if (is_numeric($item) || is_bool($item)) {
$item = '"' . $item . '"';
}
$content .= "{$key} = {$item}\n";
}
}
return file_put_contents($path, $content);
}
public static function checkField($input, $fileds)
{
$input = (array)$input;
$output = array();
foreach ($input as $k => $v) {
if (in_array($k, $fileds)) {
$output[$k] = $v;
}
}
return $output;
}
/**
* 递归合并数组,兼容 数字类型
* @param array $target
* @param mixed $source
* @return array
* @author daniel@danielsmedegaardbuus.dk
* http://www.php.net/manual/zh/function.array-merge-recursive.php
*/
public static function arrayMerge(&$target, &$source)
{
if (empty($target) OR !is_array($target)) {
$target = array();
}
if (empty($source) OR !is_array($source)) {
$source = array();
}
$merged = $target;
foreach ($source as $key => &$value) {
if (is_array($value) && isset($merged [$key]) && is_array($merged [$key])) {
$merged [$key] = self::arrayMerge($merged [$key], $value);
} else if (is_object($value) AND is_object($merged[$key])) {
$value = (array)$value;
foreach ($value as $k => $v) {
$merged [$key]->{$k} = $v;
}
} else {
$merged [$key] = $value;
}
}
return $merged;
}
/**
* 链接添加a标签
* @param string $content
* @return string
*/
public static function urlAddHref($string)
{
preg_match_all('@(https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)@', $string, $matches, PREG_SET_ORDER);
foreach ($matches as $matche) {
$url = $matche [0];
}
if ($url) {
$newUrl = '' . $url . '';
$string = str_replace($url, $newUrl, $string);
}
return $string;
}
/**
* 计算字符串长度,汉字按两个字符
* @param string $str
* @return int
*/
public static function strlenUtf8($str)
{
$length = strlen(preg_replace('/[u4e00-u9fa5 ]/', '', $str));
if ($length) {
return strlen($str) - $length + intval($length / 3) * 2;
} else {
return strlen($str);
}
}
public static function makeSemiangle($str)
{
if (!isset($str[0])) {
return null;
}
$arr = array(
'0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
'y' => 'y', 'z' => 'z',
'(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
'】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
'‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => '<',
'》' => '>', '%' => '%', '+' => '+', '—' => '-', '-' => '-',
'~' => '-', ':' => ':', '。' => '.', '、' => ',', ',' => '.',
';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
'”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',
' ' => ' ', '@' => '@', '、' => '.',);
return strtr($str, $arr);
}
/**
* 获取子串,替换全角 汉字2字符,英文1字符,数字判断
*/
public static function getUtf8SubStr($str, $len)
{
$str = self::makeSemiangle($str);
return self::getRawUtf8SubStr($str, $len);
}
/**
* 获取子串,不替换全角
*/
public static function getRawUtf8SubStr($str, $len)
{
if ((strlen($str) + mb_strlen($str, 'UTF8')) / 2 <= $len) {
return $str;
} else {
$temp_len = 0;
$temp_str = '';
for ($i = 0; $i < mb_strlen($str, 'UTF-8'); $i++) {
if ($temp_len < $len) {
$tmp = mb_substr($str, $i, 1, 'UTF-8');
$temp_str .= $tmp;
$temp_len += ((strlen($tmp) + mb_strlen($tmp, 'UTF-8')) / 2);
}
}
return $temp_str;
}
}
/**
* 从一个数组里面取出以指定key为中心的偏移, 九宫格
*/
public static function getCenterFromList($list, $key, $length = 9)
{
$list = array_values($list);
$list = array_unique($list);
$ids = array_flip($list);
$current = $ids[$key];
$total = count($list);
$first = $current - $length > 0 ? $current - $length : 0;
$last = $current + $length < $total - 1 ? $current + $length : $total - 1;
$limit = $last - $first + 1;
return array_slice($list, $first, $limit);
}
public static function safeHtmlEncodeString($str)
{
$str = htmlspecialchars_decode($str);
return htmlspecialchars($str);
}
/**
* 删除数据中指定的值
* @param max $val
* @param array $array
*/
public static function delArrayValue($val, $array)
{
$key = array_search($val, $array);
if ($key !== false) {
unset($array[$key]);
}
return array_values($array);
}
public static function strcut($str, $length, $etc = '...')
{
$result = '';
$str = html_entity_decode(trim(strip_tags($str)), ENT_QUOTES, 'UTF-8');
$strlen = strlen($str);
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++) {
$number = strpos(str_pad(decbin(ord(substr($str, $i, 1))), 8, '0', STR_PAD_LEFT), '0');
if ($number) {
if ($length < 1.0) {
break;
}
$result .= substr($str, $i, $number);
$length -= 1.0;
$i += $number - 1;
} else {
$result .= substr($str, $i, 1);
$length -= 0.5;
}
}
$result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
if ($i < $strlen) {
$result .= $etc;
}
return $result;
}
/**
* 递归调整文件属性
*/
public static function chmod($path, $filemode = 0755)
{
if (!is_dir($path)) {
return chmod($path, $filemode);
}
$dh = opendir($path);
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') {
$fullpath = $path . '/' . $file;
if (is_link($fullpath)) {
return false;
} elseif (!is_dir($fullpath) && !chmod($fullpath, $filemode)) {
return false;
} elseif (!self::chmod($fullpath, $filemode)) {
return false;
}
}
}
closedir($dh);
return chmod($path, $filemode);
}
/**
* 获取服务器IP
* @param bool $toLong 是否返回整形
* @return string
*/
public static function getServerIp($toLong = false)
{
if (isset($_SERVER['SERVER_ADDR'])) {
$ip = $_SERVER['SERVER_ADDR'];
} else {
$domain = gethostname();
$ip = gethostbyname($domain);
}
if ($toLong) {
$ip = ip2long($ip);
}
return $ip;
}
/**
* 获取客户端Ip
* @param bool $toLong 是否返回整形
* @return string|int
*/
public static function getClientIp($toLong = false)
{
$ip = '';
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && $_SERVER["HTTP_X_FORWARDED_FOR"]) {
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} elseif (isset($_SERVER["HTTP_CLIENT_IP"]) && $_SERVER["HTTP_CLIENT_IP"]) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
} elseif (isset($_SERVER["REMOTE_ADDR"]) && $_SERVER["REMOTE_ADDR"]) {
$ip = $_SERVER["REMOTE_ADDR"];
}
$pos = strpos($ip, ',');
if ($pos) {
$ip = substr($ip, 0, $pos);
}
$ip = trim($ip);
if ($toLong) {
$ip = ip2long($ip);
}
return $ip;
}
/**
* 获取截取后的文字
* 英文汉字均算做一个长度
*/
public static function getUtf8ShortCut($content, $length)
{
$shortCut = mb_substr($content, 0, $length, 'UTF-8');
$shortCut = strcmp($shortCut, $content) == 0 ? $shortCut : $shortCut . '...';
return $shortCut;
}
/**
* 检查是否有http
* @param type $str
* @return type
*/
public static function httpLinkChecker($str)
{
$reg = '/^https?/i';
if (preg_match($reg, $str)) {
return $str;
} else {
return 'http://' . $str;
}
}
/**
* 获取进程运行时间
*/
public static function getPidTime($pid)
{
$i_pinfo = file_get_contents('/proc/' . $pid . '/stat');
$args = explode(" ", $i_pinfo);
$start_time = $args[21];
$m_pinfo = file_get_contents('/proc/stat');
$args = explode("\n", $m_pinfo);
foreach ($args as $line) {
$v = trim($line);
if (strpos($line, "btime") !== false) {
$args_1 = explode(" ", $v);
$uptime = $args_1[1];
break;
}
}
if ($uptime == '' || $start_time == '') {
return 0;
}
return (int)microtime(true) - (int)($uptime + $start_time / 100);
}
/**
* 过滤标点符号
* @param type $content
* @return type
*/
public static function strap($content)
{
$content = trim($content);
$content = strip_tags($content);
$content = preg_replace("/(&[a-zA-Z]{0,4};)/u", ' ', $content);
$content = preg_replace("/([^a-zA-Z0-9\x{4e00}-\x{9fa5}]+)/u", ' ', $content);
$content = trim($content);
return $content;
}
public static function html2text($str)
{
$str = preg_replace("/