<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            php語言

            php中iconv函數使用方法

            時間:2025-05-21 05:51:12 php語言 我要投稿

            php中iconv函數使用方法

              最近在做一個程序,需要用到iconv函數把抓取來過的utf-8編碼的頁面轉成gb2312, 發現只有用iconv函數把抓取過來的數據一轉碼數據就會無緣無故的少一些。希望對大家找工作有所幫助!更多內容請關注應屆畢業生網!

              iconv函數庫能夠完成各種字符集間的轉換,是php編程中不可缺少的基礎函數庫。

              1、下載libiconv函數庫http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.9.2.tar.gz;

              2、解壓縮tar -zxvf libiconv-1.9.2.tar.gz;

              3、安裝libiconv

              #configure --prefix=/usr/local/iconv

              #make

              #make install

              4、重新編譯php 增加編譯參數--with-iconv=/usr/local/iconv

              windows下

              最近在做一個小偷程序,需要用到iconv函數把抓取來過的utf-8編碼的頁面轉成gb2312, 發現只有用iconv函數把抓取過來的數據一轉碼數據就會無緣無故的少一些。  讓我郁悶了好一會兒,去網上一查資料才知道這是iconv函數的一個bug。iconv在轉換字符"—"到gb2312時會出錯

              解決方法很簡單,就是在需要轉成的編碼后加 "//IGNORE"  也就是iconv函數第二個參數后.如下:

              以下為引用的內容:

              復制代碼 代碼如下:

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

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

              復制代碼 代碼如下:

              <?php

              echo $str= '你好,這里是賣咖啡!';

              echo '<br />';

              echo iconv('GB2312', 'UTF-8', $str); //將字符串的編碼從GB2312轉到UTF-8

              echo '<br />';

              echo iconv_substr($str, 1, 1, 'UTF-8'); //按字符個數截取而非字節

              print_r(iconv_get_encoding()); //得到當前頁面編碼信息

              echo iconv_strlen($str, 'UTF-8'); //得到設定編碼的字符串長度

              //也有這樣用的

              $content = iconv("UTF-8","gbk//TRANSLIT",$content);

              ?>

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

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

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

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

              安裝:

              #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/

              使用的簡單例子:

              <?php

              echo iconv("gb2312","ISO-8859-1","我們");

              ?>

              PHP中的mb_convert_encoding與iconv函數介紹

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

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

              mb_convert_encoding的用法見官方:

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

              做一個GBK To UTF-8

              < ?php

              header("content-Type: text/html; charset=Utf-8");

              echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK");

              ?>

              再來個GB2312 To Big5

              < ?php

              header("content-Type: text/html; charset=big5");

              echo mb_convert_encoding("你是我的朋友", "big5", "GB2312");

              ?>

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

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

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

              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 可以指定多種輸入編碼,它會根據內容自動識別,但是執行效率比iconv差太多;

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

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

              Returns the converted string or FALSE on failure.

              使用:

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

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

              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”);

              例子:

              $content = iconv(”GBK”, “UTF-8″, $content);

              $content = mb_convert_encoding($content, "UTF-8″,"GBK");

              php中使用iconv函數時容易忽略的參數

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

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

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

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

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

              Performs a character set conversion on the string str from in_charset to out_charset. Returns the converted string or FALSE on failure.

              If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character.

            【php中iconv函數使用方法】相關文章:

            分析php中iconv函數使用方法09-21

            PHP中iconv函數知識11-01

            PHP中strtotime函數使用方法詳解11-16

            php中date()日期時間函數使用方法11-11

            PHP autoload函數的使用方法10-22

            PHP校驗ISBN碼函數的使用方法10-25

            php中rename函數用法08-08

            php中cookie的使用方法09-27

            PHP中trait的使用方法08-20

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看