PHP encoded conversion function MB

Source: Internet
Author: User
Tags manual lowercase translit

After migrating a SMS interface code from Apache to NGINX+PHP-FPM, I found that I could not send a text message to see the PHP log,

[25-sep-2014 20:15:21] WARNING: [Pool www] child 9617 said in stderr: "notice:php message:php Fatal error:call to undefined function mb_conv Ert_encoding () in/data/htdocs/xx.php on line 13″

found that the function mb_convert_encoding is not defined, looking like a module is not installed, Google put, to install a mbstring expansion, before a lot of expansion (although I do not know what this extension is doing, according to the network document), is now to apply which one to install which, The force is slightly improved (at least that will let me know which module is what).

Mb-convert-encoding

String mb_convert_encoding (String $str, String $to _encoding [, mixed $from _encoding = mb_internal_encoding ()])

Converts the character encoding of string type str from an optional from_encoding to a to_encoding.

Official website document http://php.net/manual/zh/function.mb-convert-encoding.php need to install mbstring extension Library, if already compiled good PHP can be so hot compiled

The code is as follows


cd/tmp/php-5.3.28/ext/mbstring/
Usr/local/services/php/bin/phpize
./configure--with-php-config=/usr/local/services/php/bin/php-config
Make && make install
Vim/usr/local/services/php/etc/php.ini
Extension= "/usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626/mbstring.so";
Iconv
String Iconv (String in_charset, String out_charset, String str)

Iconv function Library can complete the conversion between different character sets

Note: The second parameter, in addition to specifying the encoding to be converted, can add two suffixes://translit and//ignore, where//translit automatically converts characters that cannot be directly converted into one or more approximate characters,//ignore Ignores characters that cannot be converted, and the default effect is to truncate from the first illegal character.

Returns the converted string or FALSE on Failu

Official website Address http://php.net/manual/zh/book.iconv.php

Already installed PHP, you can also use the above method to install the Iconv module

mb_convert_encoding Example

Mb_convert_encoding This function is used to convert the encoding. English generally does not exist coding problems, only Chinese data will have this problem. For example, when you use Zend Studio or EditPlus to write the program, using the GBK code, if the data needs to enter the database, and the database code for UTF8, then the data will be encoded conversion, or into the database would become garbled

Make a GBK to UTF-8

The code is as follows
<?php
Header ("content-type:text/html; Charset=utf-8″);
Echo mb_convert_encoding ("??? s my friend "," Utf-8″, "GBK");
?>

And a GB2312 to Big5.

The code is as follows

<?php
Header ("content-type:text/html; Charset=big5″);
Echo mb_convert_encoding ("You Are my Friend", "Big5″," Gb2312″);
?>

Mb_strtolower () – Make string lowercase

Mb_strtoupper () – Capitalize the string

Strtolower () – Converts a string to lowercase

Strtoupper () – Converts a string to uppercase

Ucfirst () – Converts the first letter of a string to uppercase

Ucwords () – Converts the first letter of each word in the string to uppercase

Iconv Example

Replace the gb2312 into Utf-8:

The code is as follows

$text =iconv ("GB2312", "UTF-8", $text);

In the process of using $text=iconv ("Utf-8″," Gb2312″, $text), if you encounter some special characters, such as: "-", in the English name of "." And so on characters, the conversion is broken off. The text after these characters cannot continue to convert.

For this problem, you can use the following code to implement:

The code is as follows

$text =iconv ("UTF-8", "GBK", $text);

You are not wrong, it is so simple, do not use gb2312, and written GBK, it can be.

There is also a method, the second parameter, plus//ignore, that ignores the error, as follows:

The code is as follows

Iconv ("UTF-8", "Gb2312//ignore", $data);

In general, the Mb_convert_encoding function is used only when the ICONV is encountered that cannot determine what encoding the original encoding is, or if the iconv is not displayed properly after conversion.

The code is as follows


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.