Custom Helper for PHP

if ( ! function_exists('random_code'))
{
	function random_code($size) //starting with letter
	{
	    $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
	    srand((double)microtime()*1000000); 
	    $i = 0; 
	    $pass = substr($chars, rand(0,25),1); 
	    while ($i <= ($size-1)) { 
	        $num = rand() % 33; 
	        $tmp = substr($chars, $num, 1);
	        $pass =  $pass.$tmp; 
	        $i++; 
	    } 
	    return $pass; 
	} 
}

if(!function_exists('timestamp_string'))
{
	function timestamp_string($timestamp=0)
	{
		if($timestamp==0)
		{
			$timestamp = time();
		}
		return date('Y-m-d H:i:s',$timestamp);
	}
}

if(!function_exists('order_strings'))
{
	function reorder_strings(&$str1, &$str2)
	{
		if(strcmp($str1, $str2)>0)//smalling string always goes first
		{
			$temp = $str1;
			$str1 = $str2;
			$str2 = $temp;
			return true;
		}
		return false;
	}
}

if(!function_exists('logg'))
{
	function logg($value, $stop=true)
	{
		if(is_array($value) == true)
		{
			print('<pre>');
			print_r($value);
			print('</pre>');
		}else if(is_object($value) == true){
			print('<pre>');
			print_r($value);
			print('</pre>');
		}else{
			echo var_dump($value);
		}

		if($stop == true)
		{
			exit();
		}
	}
}


if(!function_exists('val')){
	function val($object, $key, $default_value='', $decode_json=false){
		if(isset($object) == false){
			return $default_value;
		}

		if(is_object($object) == true){
			if(isset($object->{$key}) == true){
				$value = $object->{$key};
				if($decode_json == true){
					$value = json_decode($value);
				}
				return $value;
			}else{
				return $default_value;
			}
		}

		if(is_array($object) == true){
			if(isset($object[$key]) == true){
				$value = $object[$key];
				if($decode_json == true){
					$value = json_decode($value);
				}
				return $value;
			}else{
				return $default_value;
			}
		}
	}
}


if ( ! function_exists('show_alert'))
{
	function show_alert($msg, $url='') 
	{
		echo '<html><head><meta charset="UTF-8">';
		echo '<script>alert("' . $msg . '");';

		if($url != '' && strlen($url) > 0){
			echo 'window.location.href = "' . $url . '";';
		}else{
			echo 'window.history.back();';
		}

		echo '</script>';
		echo '</head><body></body></html>';
		exit();
	}
}


if(!function_exists('format_time')){
	function format_time($time, $show_time=true){
		if($show_time == true){
			return date('Y-m-d H:i:s',$time);
		}else{
			return date('Y-m-d',$time);
		}
	}
}

if(!function_exists('in_obj_array')){
	function in_obj_array($id, $array){
		foreach($array as $key => $item){
			if($item->id == $id){
				return true;
			}
		}

		return false;
	}
}

if(!function_exists('get_ip')){
	function get_ip(){
		if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
	    {
	      $ip=$_SERVER['HTTP_CLIENT_IP'];
	    }
	    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
	    {
	      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
	    }
	    else
	    {
	      $ip=$_SERVER['REMOTE_ADDR'];
	    }
	    return $ip;
	}
}

if(!function_exists('arr_value')){
	function arr_value($arr, $key, $default=''){
		if(isset($arr) == false || isset($arr[$key]) == false){
			return $default;
		}else{
			return $arr[$key];
		}
	}
}

if(!function_exists('is_json')){
	function is_json($str){
		json_decode($str);
		return (json_last_error() == JSON_ERROR_NONE);
	}
}

if(!function_exists('get_json_error')){
	function get_json_error(){
		//return '';
		switch (json_last_error()) {
			case JSON_ERROR_NONE:
				return '';
				break;
			case JSON_ERROR_DEPTH:
				return ' - Maximum stack depth exceeded';
				break;
			case JSON_ERROR_STATE_MISMATCH:
				return ' - Underflow or the modes mismatch';
				break;
			case JSON_ERROR_CTRL_CHAR:
				return ' - Unexpected control character found';
				break;
			case JSON_ERROR_SYNTAX:
				return ' - Syntax error, malformed JSON';
				break;
			case JSON_ERROR_UTF8:
				return ' - Malformed UTF-8 characters, possibly incorrectly encoded';
				break;
			default:
				return ' - Unknown error';
				break;
		}
	}
}

if(!function_exists('pad')){
	function pad($str, $digit=2, $chr='0'){
		$str = str_pad($str, $digit, $chr, STR_PAD_LEFT);
	    return $str;
	}
}

//全形字元轉半形
if(!function_exists('f2h')){
	function f2h($str){
		//全形英數字及符號
		$f=array(" ","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","~","!","@","#","$","%","^","&","*","(",")","_","+","|","‘","-","=","\","{","}","〔","〕",":","”",";","’","<",">","?",",",".","/");
		//半形英數字及符號
		$h=array(" ","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","~","!","@","#","\$","%","^","&","*","(",")","_","+","|","`","-","=","\\\\","{","}","[","]",":","\"",";","'","<",">","?",",",".","/");//注意"\\\\"為四條
		
		return str_replace($f,$h,$str);
	}
}

if(!function_exists('check_mobile_number')){
	function check_mobile_number($str){
		$pattern = '/^09\d{8}$/';
		preg_match($pattern, $str, $match);
		if(isset($match) && $match != false){
			return true;
		}else{
			return false;
		}
	}
}

if(!function_exists('check_email')){
	function check_email($email){
		if(filter_var($email, FILTER_VALIDATE_EMAIL)){
			return true;
		}else{
			return false;
		}
	}
}

if(!function_exists('clear_cookie')){
	function clear_cookie($name){
		if(isset($_COOKIE[$name]) == true){
			unset($_COOKIE[$name]);
	    	setcookie($name, null, -1, '/');
		}
	}
}

if(!function_exists('vpath')){
	function vpath($uri){
		$path = FCPATH . 'wikirex_app/views/' . $uri;
		return $path;
	}
}