mb_convert_encoding

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

mb_convert_encodingConvert character encoding

Description

mb_convert_encoding ( array|string $string , string $to_encoding [, array|string|null $from_encoding = null ] ) : array|string|false

Converts the character encoding of string to to_encoding from optionally from_encoding. If string is an array, all its string values will be converted recursively.

Parameters

string

The string or array being encoded.

to_encoding

The type of encoding that string is being converted to.

from_encoding

Is specified by character code names before conversion. It is either an array, or a comma separated enumerated list. If from_encoding is not specified, the internal encoding will be used.

See supported encodings.

Return Values

The encoded string or array on success, or false on failure.

Changelog

Version Description
8.0.0 from_encoding is nullable now.
7.2.0 This function now also accepts an array as string. Formerly, only strings have been supported.

Examples

Example #1 mb_convert_encoding() example

<?php
/* Convert internal character encoding to SJIS */
$str = mb_convert_encoding($str, "SJIS");

/* Convert EUC-JP to UTF-7 */
$str = mb_convert_encoding($str, "UTF-7", "EUC-JP");

/* 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");
?>

See Also

© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/function.mb-convert-encoding.php