1000) $distanceText = number_format(round($distance / 1000, 2), 2) . 'Km';
return $distanceText;
}
function human_filesize($bytes, $decimals = 2)
{
if ($bytes == 0) return $bytes;
$s = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
if ($bytes) {
$e = floor(log($bytes) / log(1024));
return sprintf("%.{$decimals}f ".$s[$e], ($bytes/pow(1024, floor($e))));
} else {
return 0;
}
}
function filesize_mb($bytes)
{
return $bytes / pow(1024, 2);
}
function removeHyphen($phone) {
return trim(str_replace("-", "", $phone));
}
function filterPhone($phone) {
$search = ["-", "#", "+", ".", ",", " ", "(", ")"];
$phone = str_replace($search, "", $phone);
return trim($phone);
}
function trimSignoutText($text) {
$trext = preg_replace("/#[0-9]+/", "", $text);
return trim($trext);
}
function formatPhone($phone) {
$phone = preg_replace("/[^0-9]/", "", $phone);
$length = strlen($phone);
switch($length){
case 11 :
return preg_replace("/([0-9]{3})([0-9]{4})([0-9]{4})/", "$1-$2-$3", $phone);
break;
case 10:
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone);
break;
case 9:
return preg_replace("/([0-9]{2})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone);
break;
default :
return $phone;
break;
}
}
/**
* Create a Random String
*
* Useful for generating passwords or hashes.
*
* @param integer number of characters
* @param string type of random string. basic/alpha/alunum/numeric/nozero/unique/md5/encrypt/sha1
* @return string
*/
function randomString($len = 10, $type = 'alnum') {
$str = '';
switch ($type) {
case 'basic' :
$str = mt_rand();
break;
case 'alnum' :
case 'numeric' :
case 'nozero' :
case 'alpha' :
case 'allowernum' :
case 'aluppernum' :
switch ($type) {
case 'alpha' :
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'alnum' :
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'allowernum' :
$pool = '0123456789abcdefghijklmnopqrstuvwxyz';
break;
case 'aluppernum' :
$pool = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'numeric' :
$pool = '0123456789';
break;
case 'nozero' :
$pool = '123456789';
break;
}
for ($i = 0; $i < $len; $i++) {
$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
}
break;
case 'unique' :
case 'md5' :
$str = md5(uniqid(mt_rand()));
break;
case 'encrypt' :
case 'sha1' :
$str = sha1(uniqid(mt_rand(), true));
break;
default :
$str = mt_rand();
}
return $str;
}
/**
* Calculates the great-circle distance between two points, with
* the Haversine formula.
* @param float $latitudeFrom Latitude of start point in [deg decimal]
* @param float $longitudeFrom Longitude of start point in [deg decimal]
* @param float $latitudeTo Latitude of target point in [deg decimal]
* @param float $longitudeTo Longitude of target point in [deg decimal]
* @param float $earthRadius Mean earth radius in [m]
* @return float Distance between points in [m] (same as earthRadius)
*/
function distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
return $angle * $earthRadius;
}
function dateAgo($time) {
if (!$time) {
return '방금';
}
$stat = ' 전';
if ($time < 0) {
$time *= -1;
$stat = ' 후';
} // $time=abs($time);
$ago = array();
$ct = array(31536000,2592000,604800,86400,3600,60,1); // 대략(년:365일,월:30일 기준)
$tt = array('년','달','주','일','시간','분','초');
foreach ($ct as $k => $v) {
if ($n=floor($time/$v)) {
$ago[] = $n.$tt[$k];
$time-=$n*$v;
}
}
return implode(' ', array_slice($ago,0,2)) . $stat;
}
function getNamespace($model) {
if (!$model) {
return null;
}
$class_name = get_class($model);
$reflection_class = new \ReflectionClass($class_name);
return $reflection_class->getName();
}
/**
* Prepare URL
*
* Simply adds the http:// part if no scheme is included
*
* @param string $url
* @return string
*/
function prepareURL($url = '') {
if (empty($url) || $url == 'http://' || $url == 'https://') {
return '';
}
$urls = parse_url($url);
if (!$urls || !isset($urls['scheme'])) {
$url = 'http://'. $url;
}
return $url;
}
/**
* 한글요일 변환
*
* @param datetime
* @param integer
* @return string
*/
function getWeekName($date, $full = 0) {
$weeks = array('일','월','화','수','목','금','토');
$week = date('w', strtotime($date));
if ($full) {
return $weeks[$week] . '요일';
} else {
return $weeks[$week];
}
}
// 휴대폰번호 마스킹
function maskingPhone($str) {
$str = str_replace('-','',$str);
$len = mb_strlen($str, 'utf-8');
$mValue = "";
switch($len){
case 10:
$mValue = mb_substr($str,0,3)."-".mb_substr($str,3,1)."**"."-*".mb_substr($str,7,3);;
break;
case 11:
$mValue = mb_substr($str,0,3)."-".mb_substr($str,3,2)."**"."-*".mb_substr($str,8,3);
break;
case 0:
break;
}
return $mValue;
}
// ID 마스킹
function maskingID($str) {
$mValue = preg_replace('/.{3}$/', '***', $str);
return $mValue;
}
// 이메일 마스킹
function maskingEmail($str) {
//$str = preg_replace('/(?:^|@).\K|\.[^@]*$(*SKIP)(*F)|.(?=.*?\.)/', '*', $str);
$pattern = '/(\w+)(\w{3})(@.{1})(?=.*?\.)(.+)/i';
$replace = '\1***\3*\5';
//$str = preg_replace('/(\w+)(\w{3})@(\w+)/i','\1***@\3',$str);
$str = preg_replace('/(\w+)(\w{3})(@.{1})([\w*?]+)(.+)/i','\1***\3*\5',$str);
return $str;
}
// IP Address 마스킹
function maskingIP($ip, $pos = 2, $mask = "♡", $admin = false) {
if ($admin !== false) {
return $ip;
}
$arr = explode('.', $ip);
$arr[4 - $pos] = $mask;
return implode('.',array_reverse($arr));
}
// 문자열 마스킹
function strMasking($str) {
$str = str_replace('-','',$str);
$len = mb_strlen($str, 'utf-8');
$mValue = "";
switch($len){
case 2:
$mValue = mb_strcut($str, 0, 3, "UTF-8").'*';
break;
case 3:
$mValue = mb_strcut($str, 0, 3, "UTF-8").'*'.mb_strcut($str, 8, 11, "UTF-8");
break;
default:
$mValue = mb_strcut($str, 0, 3, "UTF-8").'**'.mb_strcut($str, 12, 15, "UTF-8");
break;
}
return $mValue;
}
/**
* 자동 링크 변환 함수
*
* @param string
* @return string
*/
function setAutoLink($str) {
$regex['file'] = "gz|tgz|tar|gzip|zip|rar|mpeg|mpg|exe|rpm|dep|rm|ram|asf|ace|viv|avi|mid|gif|jpg|png|bmp|eps|mov|mp4";
$regex['file'] = "(\.(". $regex['file'] .")\") target=\"_blank\"";
$regex['http'] = "(http|https|ftp|telnet|news|mms):\/\/(([\xA1-\xFEa-z0-9:_\-]+\.[\xA1-\xFEa-z0-9,:;=_~%\[\]?\/.,+\-]+)([.]*[\/a-z0-9\[\]]|=[\xA1-\xFE]+))";
$regex['mail'] = "([\xA1-\xFEa-z0-9_.-]+)@([\xA1-\xFEa-z0-9_-]+\.[\xA1-\xFEa-z0-9._-]*[a-z]{2,3}(\?[\xA1-\xFEa-z0-9=&\?]+)*)";
// < 로 시작해서 3줄뒤에 > 가 나올 경우와
// IMG tag 와 A tag 의 경우 링크가 여러줄에 걸쳐 이루어져 있을 경우
// 이를 한줄로 합침 (합치면서 부가 옵션들은 모두 삭제함)
$src[] = "/<([^<>\n]*)\n([^<>\n]+)\n([^<>\n]*)>/i";
$tar[] = "<\\1\\2\\3>";
$src[] = "/<([^<>\n]*)\n([^\n<>]*)>/i";
$tar[] = "<\\1\\2>";
$src[] = "/<(A|IMG)[^>]*(HREF|SRC)[^=]*=[ '\"\n]*(". $regex['http'] ."|mailto:". $regex['mail'] .")[^>]*>/i";
$tar[] = "<\\1 \\2=\"\\3\">";
// email 형식이나 URL 에 포함될 경우 URL 보호를 위해 @ 을 치환
$src[] = "/(http|https|ftp|telnet|news|mms):\/\/([^ \n@]+)@/i";
$tar[] = "\\1://\\2_HTTPAT_\\3";
// 특수 문자를 치환 및 html사용시 link 보호
$src[]= "/&(quot|gt|lt)/i";
$tar[] = "!\\1";
// 3.0.11 에서 추가
$src[] = "/"/i";
$tar[] = "\"";
$src[] = "/'/i";
$tar[] = "'";
$src[] = "/}/";
$tar[] = "}";
$src[] = "/]*)href=[\"' ]*(". $regex['http'] .")[\"']*[^>]*>/i";
$tar[] = "";
$src[] = "/href=[\"' ]*mailto:(". $regex['mail'] .")[\"']*>/i";
$tar[] = "HREF=\"mailto:\\2#-#\\3\">";
$src[] = "/<([^>]*)(background|codebase|src)[ \n]*=[\n\"' ]*(". $regex['http'] .")[\"']*/i";
$tar[] = "<\\1\\2=\"\\4_orig://\\5\"";
// 링크가 안된 url및 email address 자동링크
$src[] = "/((SRC|HREF|BASE|GROUND)[ ]*=[ ]*|[^=]|^)(". $regex['http'] .")/i";
$tar[] = "\\1\\3";
$src[] = "/(". $regex['mail'] .")/i";
$tar[] = "\\1";
$src[] = "/]+>(]+>)/i";
$tar[] = "\\1";
$src[] = "/<\/A><\/A>/i";
$tar[] = "";
// 보호를 위해 치환한 것들을 복구
$src[] = "/!(quot|gt|lt)/i";
$tar[] = "&\\1";
$src[] = "/(http|https|ftp|telnet|news|mms)_orig/i";
$tar[] = "\\1";
$src[] = "'#-#'";
$tar[] = "@";
$src[] = "/". $regex['file'] ."/i";
$tar[] = "\\1";
// email 주소를 변형한 뒤 URL 속의 @ 을 복구
$src[] = "/_HTTPAT_/";
$tar[] = "@";
// 이미지에 보더값 0 을 삽입
$src[] = "/<(IMG SRC=\"[^\"]+\")>/i";
$tar[] = "<\\1 BORDER=0>";
$str = preg_replace($src,$tar,$str);
return $str;
}
/**
* 검색어 스타일 변환
*
* @param string 검색어
* @param string 원본문자열
* @return string
*/
function setSearchStyle($word = '', $str) {
if (!trim($word)) {
return $str;
}
// 검색어 전체를 공백으로 나눈다
$words = explode(' ', $word);
$pattern = '';
$bar = '';
// "/(검색1|검색2)/i" 와 같은 패턴을 만듬
foreach ($words as $val) {
if (trim($val) == '') continue;
$tmp_str = quotemeta($val);
$tmp_str = str_replace(array("/", "|"), array("\/", "\|"), $tmp_str);
$pattern .= $bar . $tmp_str . "(?![^<]*>)";
$bar = "|";
}
$replace = "\\1";
return preg_replace("/(". $pattern .")/i", $replace, $str);
}
function setDefault($val) {
if ($val === null) {
return '';
}
return $val;
}