av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語言

PHP中iconv函數(shù)知識

時間:2025-01-28 07:34:29 php語言 我要投稿
  • 相關(guān)推薦

PHP中iconv函數(shù)知識

  iconv函數(shù)庫能夠完成各種字符集間的轉(zhuǎn)換,是php編程中不可缺少的基礎(chǔ)函數(shù)庫。下面是小編為大家搜集整理出來的有關(guān)于PHP中iconv函數(shù)知識,有需要的小伙伴可以參考下。

PHP中iconv函數(shù)知識

  今天在修改論文在線的時候,遇到了iconv這個函數(shù)。學(xué)習(xí)一下

1
2
3
4
header('Content-Type: application/vnd.ms-excel;charset=UTF-8"');
$name=iconv('utf-8', 'gb2312', $data['year'].'年,第'.$data['period'].'期通信錄');
header('Content-Disposition: attachment;filename="' . $name . '.xls"');
header('Cache-Control: max-age=0');

  這段代碼的意思,就是把utf-8格式轉(zhuǎn)化為gb2312的格式,然后賦值給$name,這樣導(dǎo)出excel文件的名字的時候,就是中文的$name的名字。

  下邊是關(guān)于這個函數(shù)的詳細和延伸用法

1
iconv("UTF-8","GB2312//IGNORE",$data)

  ignore的意思是忽略轉(zhuǎn)換時的錯誤,如果沒有ignore參數(shù),所有該字符后面的字符串都無法被保存。

  這個iconv()這個函數(shù),在php5中是內(nèi)置的.謝謝.

  例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
 echo $str= '你好,這里是賣咖啡!';
 echo '<br />';
 echo iconv('GB2312', 'UTF-8', $str);   //將字符串的編碼從GB2312轉(zhuǎn)到UTF-8
 echo '<br />';
  
 echo iconv_substr($str, 1, 1, 'UTF-8');  //按字符個數(shù)截取而非字節(jié)    
 print_r(iconv_get_encoding());      //得到當前頁面編碼信息
 
 echo iconv_strlen($str, 'UTF-8');     //得到設(shè)定編碼的字符串長度
 
//也有這樣用的
 
 $content = iconv("UTF-8","gbk//TRANSLIT",$content);
 
?>

  iconv不是php的默認函數(shù),也是默認安裝的模塊。需要安裝才能用的。

  如果是windows2000+php,你可以修改php.ini文件,將extension=php_iconv.dll前的";"去掉,同時你要copy你的原php安裝文件下的iconv.dll到你的winnt/system32下(如果你的dll指向的是這個目錄)

  在linux環(huán)境下,用靜態(tài)安裝的方式,在configure時加多一項 --with-iconv就可以了,phpinfo看得到iconv的項。(Linux7.3+Apache4.06+php4.3.2),

  下載:ftp://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.8.tar.gz

  安裝:

1
2
3
4
5
#cp libiconv-1.8.tar.gz /usr/local/src
#tar zxvf lib*
#./configure --prefix=/usr/local/libiconv
#make
#make install

  編譯php

  #./configure --prefix=/usr/local/php4.3.2 --with-iconv=/usr/local/libiconv/

  使用的簡單例子:

1
2
3
<?php
 echo iconv("gb2312","ISO-8859-1","我們");
?>

  PHP中的mb_convert_encoding與iconv函數(shù)介紹

  mb_convert_encoding這個函數(shù)是用來轉(zhuǎn)換編碼的。原來一直對程序編碼這一概念不理解,不過現(xiàn)在好像有點開竅了。

  不過英文一般不會存在編碼問題,只有中文數(shù)據(jù)才會有這個問題。比如你用Zend Studio或Editplus寫程序時,用的是gbk編碼,如果數(shù)據(jù)需要入數(shù)據(jù)庫,而數(shù)據(jù)庫的編碼為utf8時,這時就要把數(shù)據(jù)進行編碼轉(zhuǎn)換,不然進到數(shù)據(jù)庫就會變成亂碼。

  mb_convert_encoding的用法見官方:

  http://cn.php.net/manual/zh/function.mb-convert-encoding.php

  做一個GBK To UTF-8

1
2
3
4
< ?php
 header("content-Type: text/html; charset=Utf-8");
 echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK");
?>

  再來個GB2312 To Big5

1
2
3
4
< ?php
 header("content-Type: text/html; charset=big5");
 echo mb_convert_encoding("你是我的朋友", "big5", "GB2312");
?>

  不過要使用上面的函數(shù)需要安裝但是需要先enable mbstring 擴展庫。

  PHP中的另外一個函數(shù)iconv也是用來轉(zhuǎn)換字符串編碼的,與上函數(shù)功能相似。

  下面還有一些詳細的例子:

1
2
3
4
iconv — Convert string to requested character encoding
(PHP 4 >= 4.0.5, PHP 5)
mb_convert_encoding — Convert character encoding
(PHP 4 >= 4.0.6, PHP 5)

  用法:

  string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )

  需要先enable mbstring 擴展庫,在 php.ini里將; extension=php_mbstring.dll 前面的 ; 去掉

  mb_convert_encoding 可以指定多種輸入編碼,它會根據(jù)內(nèi)容自動識別,但是執(zhí)行效率比iconv差太多;

  string iconv ( string in_charset, string out_charset, string str )

  注意:第二個參數(shù),除了可以指定要轉(zhuǎn)化到的編碼以外,還可以增加兩個后綴://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 會自動將不能直接轉(zhuǎn)化的字符變成一個或多個近似的字符,//IGNORE 會忽略掉不能轉(zhuǎn)化的字符,而默認效果是從第一個非法字符截斷。

  Returns the converted string or FALSE on failure.

  使用:

  發(fā)現(xiàn)iconv在轉(zhuǎn)換字符”—”到gb2312時會出錯,如果沒有ignore參數(shù),所有該字符后面的字符串都無法被保存。不管怎么樣,這個”—”都無法轉(zhuǎn)換成功,無法輸出。 另外mb_convert_encoding沒有這個bug.

  一般情況下用 iconv,只有當遇到無法確定原編碼是何種編碼,或者iconv轉(zhuǎn)化后無法正常顯示時才用mb_convert_encoding 函數(shù).

  from_encoding is specified by character code name before conversion. it can be array or string - comma separated enumerated list. If it is not specified, the internal encoding will be used.

  /* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */

  $str = mb_convert_encoding($str, “UCS-2LE”, “JIS, eucjp-win, sjis-win”);

  /* “auto” is expanded to “ASCII,JIS,UTF-8,EUC-JP,SJIS” */

  $str = mb_convert_encoding($str, “EUC-JP”, “auto”);

  例子:

1
2
$content = iconv(”GBK”, “UTF-8″, $content);
$content = mb_convert_encoding($content, "UTF-8″,"GBK");

  php中使用iconv函數(shù)時容易忽略的參數(shù)

  今天在處理抓取內(nèi)容的時候,當采用iconv進行編碼轉(zhuǎn)換的時候,發(fā)現(xiàn)結(jié)果會中斷,猜是字符集的問題,考慮怎么跳過目標字符集不存在的字符,查手冊發(fā)現(xiàn)iconv的函數(shù)只有三個參數(shù),好像不行,然后查網(wǎng)上有人說可以,但是很奇怪怎么實現(xiàn),最后發(fā)現(xiàn)英文描述有說可以加標識到目標編碼后面:“TRANSLIT”,很郁悶怎么加呢?原來是先加“//”,真是郁悶,竟然有這樣的設(shè)計

  原型: $txtContent = iconv("utf-8",'GBK',$txtContent);

  特殊參數(shù):iconv("UTF-8","GB2312//IGNORE",$data)

  兩個可選的輔助參數(shù):TRANSLIT和IGNORE ,(其中IGNORE 就是說遇到無法轉(zhuǎn)換的就跳過)。

  以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。


【PHP中iconv函數(shù)知識】相關(guān)文章:

php中iconv函數(shù)使用方法08-23

分析php中iconv函數(shù)使用方法09-21

PHP數(shù)組函數(shù)知識10-24

PHP函數(shù)知識總結(jié)09-29

關(guān)于PHP數(shù)組函數(shù)知識09-16

php中rename函數(shù)用法08-08

基于PHP中的常用函數(shù)精粹07-19

php中in-array函數(shù)用法分析10-03

PHP中strtotime函數(shù)使用方法詳解11-16