php的mb_convert_encoding函数

提示此函数未定义,"Call to undefined function:mb_convert_encoding()",

解决办法:

需要加载php_mbstring扩展

windows下修改php.ini文件 , 把;extension=php_mbstring.dll前的分号去掉

下载php_mbstring.dll  放到php.ini的同级目录

重起apache



curl设置编码

<?

#FUNCION GET OBTIENE EL CODE SOURCE

function GET($url) {

$curl = curl_init();

$header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";

$header[] = "Cache-Control: max-age=0";

$header[] = "Connection: keep-alive";

$header[] = "Keep-Alive: 300";

$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";

$header[] = "Accept-Language: en-us,en;q=0.5";

$header[] = "Pragma: ";

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/2008111317  Firefox/3.0.4');

curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');

curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');

curl_setopt($curl, CURLOPT_AUTOREFERER, true);

curl_setopt($curl, CURLOPT_COOKIEJAR, "my_cookies.txt");

curl_setopt($curl, CURLOPT_COOKIEFILE, "my_cookies.txt");

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_TIMEOUT, 10);

$html = curl_exec($curl);

curl_close($curl);

return $html;

}


function POST($mail,$sid){

$curl = curl_init();

$post = "user=".$mail."%40spamavert.com&sc=".$sid;

$header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";

$header[] = "Cache-Control: max-age=0";

$header[] = "Connection: keep-alive";

$header[] = "Keep-Alive: 300";

$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";

$header[] = "Accept-Language: en-us,en;q=0.5";

$header[] = "Pragma: ";

curl_setopt($curl, CURLOPT_URL, "http://password.esp.am/index.php?action=reminder;sa=mail");

curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/2008111317  Firefox/3.0.4');

curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

curl_setopt($curl, CURLOPT_REFERER, 'http://password.esp.am/index.php?action=reminder');

curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');

curl_setopt($curl, CURLOPT_AUTOREFERER, true);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_TIMEOUT, 10);

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_COOKIEJAR, "my_cookies.txt");

curl_setopt($curl, CURLOPT_COOKIEFILE, "my_cookies.txt");

curl_setopt($curl, CURLOPT_POSTFIELDS,$post);

$result= curl_exec ($curl);

curl_close ($curl);

return $result;

}

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