846   PHP

有几种实现方式:
1,使用系统自带的函数strrev(),适合ASCII编码的英文字符串,中文字符会出现乱码报错;
2,自己实现一个字符串翻转,通过ord()函数判断是否为中文,然后中文字符根据编码做特殊处理;

 

 

实例代码如下:

// 自己实现的字符串倒序函数
function strrev_ext($str)  
{  
    // 判断字符编码
	$encode = mb_detect_encoding($str, array('ASCII','UTF-8','GB2312','GBK')); 
	switch($encode){
		case 'ASCII':
			$byte_num=1;
			break;
		case 'UTF-8':
			// 中文 UTF-8 编码占 3 个字节
			$byte_num=3;
			break;
		case 'GB2312':
		case 'GBK':
		case 'EUC-CN':
			// 中文 GB2312,GBK 编码占 2 个字节
			$byte_num=2;
			break;
		default:
			$byte_num=1;
			break;
	}
    if (is_string($str)) {  
        $len = strlen($str);  
        $newstr = "";  
        for ($i = $len - 1; $i >=0; $i--) {  
			// 是否中文
            if(ord($str[$i]) > 127){ 
                for($j=$byte_num-1;$j>=0;$j--){
					$k=$i-$j;
					$newstr .= $str[$k];
				}
				$i=$i-($byte_num-1);
            }else{  
                $newstr.=$str[$i];  
            }  
        }  
        return $newstr;  
    }else{  
        return false;  
    }  
} 





$str_en="Hello World!";
$str_ch="世界,你好!";
$str_en_ch=$str_en.$str_ch;

echo "\n";
echo "encode:";
echo "\n";
echo $encode = mb_detect_encoding($str_en_ch, array('ASCII','UTF-8','GB2312','GBK'));
echo "\n";
echo iconv($encode,'UTF-8',$str_en_ch);


echo "\n";
echo "strrev:";
echo "\n";
echo iconv($encode,'UTF-8',strrev($str_en));
echo "\n";
echo iconv($encode,'UTF-8',strrev($str_ch));
echo "\n";
echo iconv($encode,'UTF-8',strrev($str_en_ch));
echo "\n";


echo "\n";
echo "strrev_ext:";
echo "\n";
echo iconv($encode,'UTF-8',strrev_ext($str_en)); 
echo "\n";
echo iconv($encode,'UTF-8',strrev_ext($str_ch));
echo "\n";
echo iconv($encode,'UTF-8',strrev_ext($str_en_ch));
echo "\n";

 

 

测试结果如下:

 

1,UTF-8编码的字符串

chenyunhui@ubuntu:/mnt/hgfs/ShareFolder/web/click$ php test.php

encode:
UTF-8
Hello World!世界,你好!
strrev:
!dlroW olleH
PHP Notice:  iconv(): Detected an illegal character in input string in /mnt/hgfs/ShareFolder/web/click/test.php on line 67
PHP Stack trace:
PHP   1. {main}() /mnt/hgfs/ShareFolder/web/click/test.php:0
PHP   2. iconv() /mnt/hgfs/ShareFolder/web/click/test.php:67

PHP Notice:  iconv(): Detected an illegal character in input string in /mnt/hgfs/ShareFolder/web/click/test.php on line 69
PHP Stack trace:
PHP   1. {main}() /mnt/hgfs/ShareFolder/web/click/test.php:0
PHP   2. iconv() /mnt/hgfs/ShareFolder/web/click/test.php:69


strrev_ext:
!dlroW olleH
!好你,界世
!好你,界世!dlroW olleH

 

 

2,GBK字符串编码

 

 

chenyunhui@ubuntu:/mnt/hgfs/ShareFolder/web/click$ php test.php

encode:
EUC-CN
Hello World!世界,你好!
strrev:
!dlroW olleH
PHP Notice:  iconv(): Detected an illegal character in input string in /mnt/hgfs/ShareFolder/web/click/test.php on line 67
PHP Stack trace:
PHP   1. {main}() /mnt/hgfs/ShareFolder/web/click/test.php:0
PHP   2. iconv() /mnt/hgfs/ShareFolder/web/click/test.php:67

PHP Notice:  iconv(): Detected an illegal character in input string in /mnt/hgfs/ShareFolder/web/click/test.php on line 69
PHP Stack trace:
PHP   1. {main}() /mnt/hgfs/ShareFolder/web/click/test.php:0
PHP   2. iconv() /mnt/hgfs/ShareFolder/web/click/test.php:69


strrev_ext:
!dlroW olleH
!好你,界世
!好你,界世!dlroW olleH



Leave a Reply

Your email address will not be published. Required fields are marked *