local.php

来自「php 开发的内容管理系统」· PHP 代码 · 共 188 行

PHP
188
字号
<?php
// $Id: local.php,v 1.1.2.5 2005/09/16 01:16:50 phppp Exp $

$original_locale = setlocale(LC_ALL, 0);
if (!setlocale(LC_ALL, 'zh')) {
	setlocale(LC_ALL, $original_locale );
}

// !!IMPORTANT!! insert '\' before any char among reserved chars: "a", "A", "B", "c", "d", "D", "F", "g", "G", "h", "H", "i", "I", "j", "l", "L", "m", "M", "n", "O", "r", "s", "S", "t", "T", "U", "w", "W", "Y", "y", "z", "Z"	
// insert double '\' before 't', 'r', 'n'
define("_TODAY", "浠婂ぉ G:i");
define("_YESTERDAY", "鏄ㄥぉ G:i");
define("_MONTHDAY", "n/j G:i");
define("_YEARMONTHDAY", "Y/n/j G:i");

// Local handler for string and encoding
class XoopsLocal
{
	// localized substr
	function substr($str, $start, $length, $trimmarker = '...') {
	    $charset = empty($GLOBALS["xlanguage"]['charset_base'])?_CHARSET:$GLOBALS["xlanguage"]['charset_base'];
	    
	    if (function_exists('mb_internal_encoding') && @mb_internal_encoding($charset)) {
	        $str2 = mb_strcut( $str , $start , $length - strlen( $trimmarker ) );
	        return $str2 . ( mb_strlen($str) != mb_strlen($str2) ? $trimmarker : '' );
	    }
	    
	    switch(strtoupper($charset)){
	    case "UTF-8":
	    	$string = $str;
	    	$ellipsis =& $trimmarker;
	    	// From mediawiki::truncate
			$string = substr( $string, 0, $length );
			$char = ord( $string[strlen( $string ) - 1] );
			if ($char >= 0xc0) {
				# We got the first byte only of a multibyte char; remove it.
				$string = substr( $string, 0, -1 );
			} elseif( $char >= 0x80 &&
			          preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
			                      '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
			    # We chopped in the middle of a character; remove it
				$string = $m[1];
			}
			return $string . ( (strlen($str) > strlen($string)) ? $ellipsis : "" );
			break;
	    
	    default:
	    
			$ch = chr(127);
			$p = array("/[\x81-\xfe]([\x81-\xfe]|[\x40-\xfe])/", "/[\x01-\x77]/");
			$r = array("", "");
			
			if($start > 0) {
				$s = substr($str,0,$start);
				if($s{strlen($s)-1} > $ch) {
					$s = preg_replace($p,$r,$s);
					$start += strlen($s);
				}
			}
			$s = substr($str,$start,$start+$length);
			$end = strlen($s);
			if($s{$end-1} > $ch) {
				$s = preg_replace($p,$r,$s);
				$end += strlen($s);
			}
			$ret = substr($str,$start,$end);
			if($start + $length < strlen($str)){
				$ret .= $trimmarker;
			}
			
			return $ret;
			break;
		
		}
	}
	
	function utf8_encode($text)
	{
		$text = XoopsLocal::convert_encoding($text, 'utf-8');
		return $text;
	}
	
	function convert_encoding($text, $to='utf-8', $from='')
	{
		if(empty($text)) {		
			return $text;
		}
	    if(empty($from)) $from = empty($GLOBALS["xlanguage"]['charset_base'])?_CHARSET:$GLOBALS["xlanguage"]['charset_base'];
	    if (empty($to) || !strcasecmp($to, $from)) return $text;
	    
		if(function_exists('mb_convert_encoding')) {
			$converted_text = @mb_convert_encoding($text, $to, $from);
		}elseif(function_exists('iconv')) {
			$converted_text = @iconv($from, $to . "//TRANSLIT", $text);
		}	
		if(empty($converted_text)){
			static $xconv_handler;
			$xconv_handler = isset($xconv_handler)?$xconv_handler:@xoops_getmodulehandler('xconv', 'xconv', true);
			if(is_object($xconv_handler)){
				$converted_text = @$xconv_handler->convert_encoding($text, $to, $from);
				if(!empty($converted_text)) {
					return $converted_text;
				}
			}
		}
		
		$text = empty($converted_text)?$text:$converted_text;
	
	    return $text;
	}

	function trim($text)
	{
	    $ret = trim($text);
	    return $ret;
	}
	
	/*
	* Function to display formatted times in user timezone
	*/
	function formatTimestamp($time, $format="l", $timeoffset="")
	{
	    global $xoopsConfig, $xoopsUser;
	    if(strtolower($format) == "rss" ||strtolower($format) == "r"){
        	$TIME_ZONE = "";
        	if(!empty($GLOBALS['xoopsConfig']['server_TZ'])){
				$server_TZ = abs(intval($GLOBALS['xoopsConfig']['server_TZ']*3600.0));
				$prefix = ($GLOBALS['xoopsConfig']['server_TZ']<0)?" -":" +";
				$TIME_ZONE = $prefix.date("Hi",$server_TZ);
			}
			$date = gmdate("D, d M Y H:i:s", intval($time)).$TIME_ZONE;
			return $date;
    	}
    	
	    $usertimestamp = xoops_getUserTimestamp($time, $timeoffset);
	    switch (strtolower($format)) {
        case 's':
	        $datestring = _SHORTDATESTRING;
	        break;
        case 'm':
	        $datestring = _MEDIUMDATESTRING;
	        break;
        case 'mysql':
	        $datestring = "Y-m-d H:i:s";
	        break;
        case 'rss':
	    	$datestring = "r";
	        break;
        case 'l':
	        $datestring = _DATESTRING;
	        break;
        case 'c':
        case 'custom':
	        $current_timestamp = xoops_getUserTimestamp(time(), $timeoffset);
	        if(date("Ymd", $usertimestamp) == date("Ymd", $current_timestamp)){
				$datestring = _TODAY;
			}elseif(date("Ymd", $usertimestamp+24*60*60) == date("Ymd", $current_timestamp)){
				$datestring = _YESTERDAY;
			}elseif(date("Y", $usertimestamp) == date("Y", $current_timestamp)){
				$datestring = _MONTHDAY;
			}else{
				$datestring = _YEARMONTHDAY;
			}
	        break;
        default:
	        if ($format != '') {
	            $datestring = $format;
	        } else {
	            $datestring = _DATESTRING;
	        }
	        break;
	    }

	    return date($datestring, $usertimestamp);
	}
	
	
	// adding your new functions
	// calling the function:
	// Method 1: echo xoops_local("hello", "Some greeting words");
	// Method 2: echo XoopsLocal::hello("Some greeting words");
	function hello($text)
	{
		$ret = "<div>Hello, ".$text."</div>";
		return $ret;
	}
}
?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?