/**
* 将一個字串中含有全角的數字字元、字母、空格或'%+-()'字元轉換為相應半角字元
*
* @access public
* @param string $str 待轉換字串
* @return string $str 處理後字串
*/
function make_semiangle($str)
{
$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);
}
php中文截取字元串
<?php
/*
utf-8、gb2312都支援的漢字截取函數
cut_str(字元串, 截取長度, 開始長度, 編碼);
編碼預設為 utf-8
開始長度預設為 0
*/
function cut_str($string, $sublen, $start = 0, $code = 'utf-8') {
if ($code == 'utf-8') {
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all ( $pa, $string, $t_string );
if (count ( $t_string [0] ) - $start > $sublen)
return join ( '', array_slice ( $t_string [0], $start, $sublen ) ) . "...";
return join ( '', array_slice ( $t_string [0], $start, $sublen ) );
} else {
$start = $start * 2;
$sublen = $sublen * 2;
$strlen = strlen ( $string );
$tmpstr = '';
for($i = 0; $i < $strlen; $i ++) {
if ($i >= $start && $i < ($start + $sublen)) {
if (ord ( substr ( $string, $i, 1 ) ) > 129) {
$tmpstr .= substr ( $string, $i, 2 );
} else {
$tmpstr .= substr ( $string, $i, 1 );
}
}
if (ord ( substr ( $string, $i, 1 ) ) > 129)
$i ++;
}
if (strlen ( $tmpstr ) < $strlen)
$tmpstr .= "...";
return $tmpstr;
}
$str = "abcd需要截取的字元串";
echo cut_str ( $str, 1, 0, 'gb2312' );
?>
* 字元截取 支援utf8/gbk
* @param $string
* @param $length
* @param $dot
function str_cut($string, $length, $charset = 'utf-8', $dot = '...') {
$strlen = strlen($string);
if($strlen <= $length) return $string;
$string = str_replace(array(' ','&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array('∵',' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
$strcut = '';
if(strtolower($charset) == 'utf-8') {
$length = intval($length-strlen($dot)-$length/3);
$n = $tn = $noc = 0;
while($n < strlen($string)) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t <= 239) {
$tn = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6; $noc += 2;
} else {
$n++;
if($noc >= $length) {
break;
if($noc > $length) {
$n -= $tn;
$strcut = substr($string, 0, $n);
$strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), array(' ', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), $strcut);
$dotlen = strlen($dot);
$maxi = $length - $dotlen - 1;
$current_str = '';
$search_arr = array('&',' ', '"', "'", '“', '”', '—', '<', '>', '·', '…','∵');
$replace_arr = array('&amp;','&nbsp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;',' ');
$search_flip = array_flip($search_arr);
for ($i = 0; $i < $maxi; $i++) {
$current_str = ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
if (in_array($current_str, $search_arr)) {
$key = $search_flip[$current_str];
$current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);
$strcut .= $current_str;
return $strcut.$dot;
php中文字元串翻轉
通常情況下翻轉一個字元串隻需要strrev()函數就可以了,但有時我需要處理是字元串是含中文的,這樣用strrev就會出現亂碼,是以我們需要自定義一個函數來處理含中文的字元。
function cstrrev($str)
$len = strlen($str);
for($i = 0; $i < $len; $i++)
{
$char = $str{0};
if(ord($char) > 127)
{
$i++;
if($i < $len)
{
$arr[] = substr($str, 0, 2);
$str = substr($str, 2);
else
$arr[] = $char;
$str = substr($str, 1);
return join(array_reverse($arr));
#使用方法:
$str = '中文.look!';
echo cstrrev($str);
#結果輸出:!kool.文中
str_replace
function str_replace_cn($needle, $str, $haystack, $charset = "utf-8"){
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $haystack, $match_haystack);
preg_match_all($re[$charset], $needle, $match_needle);
for($i = 0; $i < count($match_needle); $i ++){
if(!in_array($match_needle[0][$i], $match_haystack[0]))return $haystack;//無比對
$match_haystack = $match_haystack[0];
$match_needle = $match_needle[0];
for($i = 0; $i < count($match_haystack); $i ++){
if($match_haystack[$i] == "")continue;
if($match_haystack[$i] == $match_needle[0]){
if(count($match_needle) == 1){//如果隻一個字元
$match_haystack[$i] = $str;
}else{
$flag = true;
for($j = 1; $j < count($match_needle); $j ++){
if($match_haystack[$i + $j] != $match_needle[$j]){
$flag = false;
break;
}
}
if($flag){//比對
$match_haystack[$i] = $str;
for($j = 1; $j < count($match_needle); $j ++){
$match_haystack[$i + $j] = "";
return implode("", $match_haystack);
* 實作多種字元編碼方式
* @param $input 需要編碼的字元串
* @param $_output_charset 輸出的編碼格式
* @param $_input_charset 輸入的編碼格式
* return 編碼後的字元串
function charsetencode($input, $_output_charset, $_input_charset) {
$output = "";
if (!isset($_output_charset)) $_output_charset = $_input_charset;
if ($_input_charset == $_output_charset || $input == null) {
$output = $input;
} elseif (function_exists("mb_convert_encoding")) {
$output = mb_convert_encoding($input, $_output_charset, $_input_charset);
} elseif (function_exists("iconv")) {
$output = iconv($_input_charset, $_output_charset, $input);
} else die("sorry, you have no libs support for charset change.");
return $output;
* 實作多種字元解碼方式
* @param $input 需要解碼的字元串
* @param $_output_charset 輸出的解碼格式
* @param $_input_charset 輸入的解碼格式
* return 解碼後的字元串
function charsetdecode($input, $_input_charset, $_output_charset) {
if (!isset($_input_charset)) $_input_charset = $_input_charset;
} else die("sorry, you have no libs support for charset changes.");
sfds