⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utf_tools.php

📁 这些都是我以前学习是用到的源码
💻 PHP
📖 第 1 页 / 共 3 页
字号:
<?php/** ** @package phpBB3* @version $Id: utf_tools.php,v 1.31 2006/11/26 13:07:43 acydburn Exp $* @copyright (c) 2006 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License ** @todo make sure the replacements are called correctly* already done: strtolower, strtoupper, ucfirst, str_split, strrpos, strlen (hopefully!), strpos, substr, htmlspecialchars* remaining:	strspn, chr, ord*//***/if (!defined('IN_PHPBB')){	exit;}/*** UTF-8 tools** Whenever possible, these functions will try to use PHP's built-in functions or* extensions, otherwise they will default to custom routines.** @package phpBB3*/if (!extension_loaded('xml')){	/**	* Implementation of PHP's native utf8_encode for people without XML support	* This function exploits some nice things that ISO-8859-1 and UTF-8 have in common	*	* @param string $str ISO-8859-1 encoded data	* @return string UTF-8 encoded data	*/	function utf8_encode($str)	{		$out = '';		for ($i = 0, $len = strlen($str); $i < $len; $i++)		{			$letter = $str[$i];			$num = ord($letter);			if ($num < 0x80)			{				$out .= $letter;			}			else if ($num < 0xC0)			{				$out .= "\xC2" . $letter;			}			else			{				$out .= "\xC3" . chr($num - 64);			}		}		return $out;	}	/**	* Implementation of PHP's native utf8_decode for people without XML support	*	* @param string $str UTF-8 encoded data	* @return string ISO-8859-1 encoded data	*/	function utf8_decode($str)	{		$pos = 0;		$len = strlen($str);		$ret = '';			while ($pos < $len)		{			$ord = ord($str[$pos]) & 0xF0;			if ($ord === 0xC0 || $ord === 0xD0)			{				$charval = ((ord($str[$pos]) & 0x1F) << 6) | (ord($str[$pos + 1]) & 0x3F);				$pos += 2;				$ret .= (($charval < 256) ? chr($charval) : '?');			}			else if ($ord === 0xE0)			{				$ret .= '?';				$pos += 3;			}			else if ($ord === 0xF0)			{				$ret .= '?';				$pos += 4;			}			else			{				$ret .= $str[$pos];				++$pos;			}		}		return $ret;	}}// mbstring is old and has it's functions around for older versions of PHP.// if mbstring is not loaded, we go into native mode.if (extension_loaded('mbstring')){	/**	* UTF-8 aware alternative to strrpos	* Find position of last occurrence of a char in a string	*	* Notes:	* - offset for mb_strrpos was added in 5.2.0, we emulate if it is lower	*/	if (version_compare(phpversion(), '5.2.0', '>='))	{		/**		* UTF-8 aware alternative to strrpos		* @ignore		*/		function utf8_strrpos($str,	$needle, $offset = null)		{			// Emulate behaviour of strrpos rather than raising warning			if (empty($str))			{				return false;			}			if (is_null($offset))			{				return mb_strrpos($str, $needle);			}			else			{				return mb_strrpos($str, $needle, $offset);			}		}	}	else	{		/**		* UTF-8 aware alternative to strrpos		* @ignore		*/		function utf8_strrpos($str,	$needle, $offset = null)		{			// offset for mb_strrpos was added in 5.2.0			if (is_null($offset))			{				// Emulate behaviour of strrpos rather than raising warning				if (empty($str))				{					return false;				}				return mb_strrpos($str, $needle);			}			else			{				if (!is_int($offset))				{					trigger_error('utf8_strrpos expects parameter 3 to be long', E_USER_WARNING);					return false;				}				$str = mb_substr($str, $offset);				if (false !== ($pos = mb_strrpos($str, $needle)))				{					return $pos + $offset;				}				return false;			}		}	}	/**	* UTF-8 aware alternative to strpos	* @ignore	*/	function utf8_strpos($str, $needle, $offset = null)	{		if (is_null($offset))		{			return mb_strpos($str, $needle);		}		else		{			return mb_strpos($str, $needle, $offset);		}	}	/**	* UTF-8 aware alternative to strtolower	* @ignore	*/	function utf8_strtolower($str)	{		return mb_strtolower($str);	}	/**	* UTF-8 aware alternative to strtoupper	* @ignore	*/	function utf8_strtoupper($str)	{		return mb_strtoupper($str);	}	/**	* UTF-8 aware alternative to substr	* @ignore	*/	function utf8_substr($str, $offset, $length = null)	{		if (is_null($length))		{			return mb_substr($str, $offset);		}		else		{			return mb_substr($str, $offset, $length);		}	}	/**	* Return the length (in characters) of a UTF-8 string	* @ignore	*/	function utf8_strlen($text)	{		return mb_strlen($text, 'utf-8');	}}else{	/**	* UTF-8 aware alternative to strrpos	* Find position of last occurrence of a char in a string	* 	* @author Harry Fuecks	* @param string $str haystack	* @param string $needle needle	* @param integer $offset (optional) offset (from left)	* @return mixed integer position or FALSE on failure	*/	function utf8_strrpos($str,	$needle, $offset = null)	{		if (is_null($offset))		{			$ar	= explode($needle, $str);						if (sizeof($ar) > 1)			{				// Pop off the end of the string where the last	match was made				array_pop($ar);				$str = join($needle, $ar);				return utf8_strlen($str);			}			return false;		}		else		{			if (!is_int($offset))			{				trigger_error('utf8_strrpos	expects	parameter 3	to be long', E_USER_WARNING);				return false;			}			$str = utf8_substr($str, $offset);			if (false !== ($pos = utf8_strrpos($str, $needle)))			{				return $pos	+ $offset;			}			return false;		}	}	/**	* UTF-8 aware alternative to strpos	* Find position of first occurrence of a string	*	* @author Harry Fuecks	* @param string $str haystack	* @param string $needle needle	* @param integer $offset offset in characters (from left)	* @return mixed integer position or FALSE on failure	*/	function utf8_strpos($str, $needle, $offset = null)	{		if (is_null($offset))		{			$ar = explode($needle, $str);			if (sizeof($ar) > 1)			{				return utf8_strlen($ar[0]);			}			return false;		}		else		{			if (!is_int($offset))			{				trigger_error('utf8_strpos:  Offset must  be an integer', E_USER_ERROR);				return false;			}			$str = utf8_substr($str, $offset);			if (false !== ($pos = utf8_strpos($str, $needle)))			{				return $pos + $offset;			}			return false;		}	}	$UTF8_UPPER_TO_LOWER = array(		"\x41" => "\x61", "\x42" => "\x62", "\x43" => "\x63", "\x44" => "\x64",		"\x45" => "\x65", "\x46" => "\x66", "\x47" => "\x67", "\x48" => "\x68",		"\x49" => "\x69", "\x4A" => "\x6A", "\x4B" => "\x6B", "\x4C" => "\x6C",		"\x4D" => "\x6D", "\x4E" => "\x6E", "\x4F" => "\x6F", "\x50" => "\x70",		"\x51" => "\x71", "\x52" => "\x72", "\x53" => "\x73", "\x54" => "\x74",		"\x55" => "\x75", "\x56" => "\x76", "\x57" => "\x77", "\x58" => "\x78",		"\x59" => "\x79", "\x5A" => "\x7A", "\xC3\x80" => "\xC3\xA0", "\xC3\x81" => "\xC3\xA1",		"\xC3\x82" => "\xC3\xA2", "\xC3\x83" => "\xC3\xA3", "\xC3\x84" => "\xC3\xA4", "\xC3\x85" => "\xC3\xA5",		"\xC3\x86" => "\xC3\xA6", "\xC3\x87" => "\xC3\xA7", "\xC3\x88" => "\xC3\xA8", "\xC3\x89" => "\xC3\xA9",		"\xC3\x8A" => "\xC3\xAA", "\xC3\x8B" => "\xC3\xAB", "\xC3\x8C" => "\xC3\xAC", "\xC3\x8D" => "\xC3\xAD",		"\xC3\x8E" => "\xC3\xAE", "\xC3\x8F" => "\xC3\xAF", "\xC3\x90" => "\xC3\xB0", "\xC3\x91" => "\xC3\xB1",		"\xC3\x92" => "\xC3\xB2", "\xC3\x93" => "\xC3\xB3", "\xC3\x94" => "\xC3\xB4", "\xC3\x95" => "\xC3\xB5",		"\xC3\x96" => "\xC3\xB6", "\xC3\x98" => "\xC3\xB8", "\xC3\x99" => "\xC3\xB9", "\xC3\x9A" => "\xC3\xBA",		"\xC3\x9B" => "\xC3\xBB", "\xC3\x9C" => "\xC3\xBC", "\xC3\x9D" => "\xC3\xBD", "\xC3\x9E" => "\xC3\xBE",		"\xC4\x80" => "\xC4\x81", "\xC4\x82" => "\xC4\x83", "\xC4\x84" => "\xC4\x85", "\xC4\x86" => "\xC4\x87",		"\xC4\x88" => "\xC4\x89", "\xC4\x8A" => "\xC4\x8B", "\xC4\x8C" => "\xC4\x8D", "\xC4\x8E" => "\xC4\x8F",		"\xC4\x90" => "\xC4\x91", "\xC4\x92" => "\xC4\x93", "\xC4\x96" => "\xC4\x97", "\xC4\x98" => "\xC4\x99",

⌨️ 快捷键说明

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