Output encoding in php

mb_http_output

Установка/получение кодировки символов HTTP-вывода. Выходные данные после работы этой функции будут преобразованы из внутренней кодировки к encoding .

Список параметров

Если задан аргумент encoding , mb_http_output() устанавливает кодировку выходных символов HTTP в encoding .

Если аргумент encoding опущен, mb_http_output() возвращает текущую кодировку символов вывода HTTP.

Возвращаемые значения

Если аргумент encoding опущен, mb_http_output() возвращает текущую кодировку символов вывода HTTP. В противном случае, Возвращает true в случае успешного выполнения или false в случае возникновения ошибки.

Список изменений

Смотрите также

  • mb_internal_encoding() — Установка/получение внутренней кодировки скрипта
  • mb_http_input() — Определение кодировки символов входных данных HTTP-запроса
  • mb_detect_order() — Установка/получение списка кодировок для механизмов определения кодировки

User Contributed Notes 2 notes

By default this is set to ‘pass’ witch means that php will not touch the encoding and output it as it is in the source file.

Normaly this function isn’t needed as long as you save the .php file in the same encoding as you want it to output as well as retrive all input in the same encoding, but it don’t seam to herd as long as it able to figure out what encoding the output is in ín the first place.

// —— set mb_http_output encoding to UTF-8 ——
mb_http_output(‘UTF-8’);

// —— setup php for working with Unicode data ——
mb_internal_encoding(‘UTF-8’);
mb_http_output(‘UTF-8’);
mb_http_input(‘UTF-8’);
mb_language(‘uni’);
mb_regex_encoding(‘UTF-8’);
ob_start(‘mb_output_handler’);

  • Функции для работы с многобайтовыми строками
    • mb_​check_​encoding
    • mb_​chr
    • mb_​convert_​case
    • mb_​convert_​encoding
    • mb_​convert_​kana
    • mb_​convert_​variables
    • mb_​decode_​mimeheader
    • mb_​decode_​numericentity
    • mb_​detect_​encoding
    • mb_​detect_​order
    • mb_​encode_​mimeheader
    • mb_​encode_​numericentity
    • mb_​encoding_​aliases
    • mb_​ereg_​match
    • mb_​ereg_​replace_​callback
    • mb_​ereg_​replace
    • mb_​ereg_​search_​getpos
    • mb_​ereg_​search_​getregs
    • mb_​ereg_​search_​init
    • mb_​ereg_​search_​pos
    • mb_​ereg_​search_​regs
    • mb_​ereg_​search_​setpos
    • mb_​ereg_​search
    • mb_​ereg
    • mb_​eregi_​replace
    • mb_​eregi
    • mb_​get_​info
    • mb_​http_​input
    • mb_​http_​output
    • mb_​internal_​encoding
    • mb_​language
    • mb_​list_​encodings
    • mb_​ord
    • mb_​output_​handler
    • mb_​parse_​str
    • mb_​preferred_​mime_​name
    • mb_​regex_​encoding
    • mb_​regex_​set_​options
    • mb_​scrub
    • mb_​send_​mail
    • mb_​split
    • mb_​str_​split
    • mb_​strcut
    • mb_​strimwidth
    • mb_​stripos
    • mb_​stristr
    • mb_​strlen
    • mb_​strpos
    • mb_​strrchr
    • mb_​strrichr
    • mb_​strripos
    • mb_​strrpos
    • mb_​strstr
    • mb_​strtolower
    • mb_​strtoupper
    • mb_​strwidth
    • mb_​substitute_​character
    • mb_​substr_​count
    • mb_​substr

    Источник

    Output encoding in php

    The behaviour of these functions is affected by settings in php.ini .

    mbstring configuration options

    Name Default Changeable Changelog
    mbstring.language «neutral» PHP_INI_ALL
    mbstring.detect_order NULL PHP_INI_ALL
    mbstring.http_input «pass» PHP_INI_ALL Deprecated
    mbstring.http_output «pass» PHP_INI_ALL Deprecated
    mbstring.internal_encoding NULL PHP_INI_ALL Deprecated
    mbstring.substitute_character NULL PHP_INI_ALL
    mbstring.func_overload «0» PHP_INI_SYSTEM Deprecated as of PHP 7.2.0; removed as of PHP 8.0.0.
    mbstring.encoding_translation «0» PHP_INI_PERDIR
    mbstring.http_output_conv_mimetypes «^(text/|application/xhtml\+xml)» PHP_INI_ALL
    mbstring.strict_detection «0» PHP_INI_ALL
    mbstring.regex_retry_limit «1000000» PHP_INI_ALL Available as of PHP 7.4.0.
    mbstring.regex_stack_limit «100000» PHP_INI_ALL Available as of PHP 7.3.5.

    For further details and definitions of the PHP_INI_* modes, see the Where a configuration setting may be set.

    Here’s a short explanation of the configuration directives.

    The default national language setting (NLS) used in mbstring. Note that this option automagically defines mbstring.internal_encoding and mbstring.internal_encoding should be placed after mbstring.language in php.ini

    Enables the transparent character encoding filter for the incoming HTTP queries, which performs detection and conversion of the input encoding to the internal character encoding.

    This deprecated feature will certainly be removed in the future.

    Defines the default internal character encoding.

    Users should leave this empty and set default_charset instead.

    This deprecated feature will certainly be removed in the future.

    Defines the default HTTP input character encoding.

    Users should leave this empty and set default_charset instead.

    This deprecated feature will certainly be removed in the future.

    Defines the default HTTP output character encoding (output will be converted from the internal encoding to the HTTP output encoding upon output).

    Users should leave this empty and set default_charset instead.

    Defines default character code detection order. See also mb_detect_order() .

    Defines character to substitute for invalid character encoding. See mb_substitute_character() for supported values.

    This feature has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. Relying on this feature is highly discouraged.

    Overloads a set of single byte functions by the mbstring counterparts. See Function overloading for more information.

    This setting can only be changed from the php.ini file.

    Enables strict encoding detection. See mb_detect_encoding() for a description and examples.

    Limits the amount of backtracking that may be performed during one mbregex match.

    This setting only takes effect when linking against oniguruma >= 6.8.0.

    Limits the stack depth of mbstring regular expressions.

    According to the » HTML 4.01 specification, Web browsers are allowed to encode a form being submitted with a character encoding different from the one used for the page. See mb_http_input() to detect character encoding used by browsers.

    Although popular browsers are capable of giving a reasonably accurate guess to the character encoding of a given HTML document, it would be better to set the charset parameter in the Content-Type HTTP header to the appropriate value by header() or default_charset ini setting.

    Example #1 php.ini setting examples

    ; Set default language mbstring.language = Neutral; Set default language to Neutral(UTF-8) (default) mbstring.language = English; Set default language to English mbstring.language = Japanese; Set default language to Japanese ;; Set default internal encoding ;; Note: Make sure to use character encoding works with PHP mbstring.internal_encoding = UTF-8 ; Set internal encoding to UTF-8 ;; HTTP input encoding translation is enabled. mbstring.encoding_translation = On ;; Set default HTTP input character encoding ;; Note: Script cannot change http_input setting. mbstring.http_input = pass ; No conversion. mbstring.http_input = auto ; Set HTTP input to auto ; "auto" is expanded according to mbstring.language mbstring.http_input = SJIS ; Set HTTP input to SJIS mbstring.http_input = UTF-8,SJIS,EUC-JP ; Specify order ;; Set default HTTP output character encoding mbstring.http_output = pass ; No conversion mbstring.http_output = UTF-8 ; Set HTTP output encoding to UTF-8 ;; Set default character encoding detection order mbstring.detect_order = auto ; Set detect order to auto mbstring.detect_order = ASCII,JIS,UTF-8,SJIS,EUC-JP ; Specify order ;; Set default substitute character mbstring.substitute_character = 12307 ; Specify Unicode value mbstring.substitute_character = none ; Do not print character mbstring.substitute_character = long ; Long Example: U+3000,JIS+7E7E

    Example #2 php.ini setting for EUC-JP users

    ;; Disable Output Buffering output_buffering = Off ;; Set HTTP header charset default_charset = EUC-JP ;; Set default language to Japanese mbstring.language = Japanese ;; HTTP input encoding translation is enabled. mbstring.encoding_translation = On ;; Set HTTP input encoding conversion to auto mbstring.http_input = auto ;; Convert HTTP output to EUC-JP mbstring.http_output = EUC-JP ;; Set internal encoding to EUC-JP mbstring.internal_encoding = EUC-JP ;; Do not print invalid characters mbstring.substitute_character = none

    Example #3 php.ini setting for SJIS users

    ;; Enable Output Buffering output_buffering = On ;; Set mb_output_handler to enable output conversion output_handler = mb_output_handler ;; Set HTTP header charset default_charset = Shift_JIS ;; Set default language to Japanese mbstring.language = Japanese ;; Set http input encoding conversion to auto mbstring.http_input = auto ;; Convert to SJIS mbstring.http_output = SJIS ;; Set internal encoding to EUC-JP mbstring.internal_encoding = EUC-JP ;; Do not print invalid characters mbstring.substitute_character = none

    User Contributed Notes 3 notes

    String literals in the PHP script are encoded with the same encoding that the PHP file was saved with. This is not affected by default_charset or other .ini settings.

    Scenario: The default_charset is KOI8-R, and there is a text file «input.txt» containing the string «Это текст для поиска.» in KOI8-R encoding.

    $data = file_get_contents ( ‘input.txt’ );

    echo mb_strpos ( $data , $string );

    ?>
    But unfortunately it was saved as UTF-8.

    It doesn’t work; mb_strpos() returns false because it can’t find the UTF-8-encoded «текст» inside the KOI8-R-encoded «Это текст для поиска.».

    Adjusting the default_charset had no effect. Not even fiddling with mb_internal_encoding could fix it, simply because the strings involved had *different* encodings and without actually changing one of them they just weren’t going to match.

    Either re-save the source file as KOI8-R to match the data file, or re-save the data file as UTF-8 to match the source code. Only then will the script properly echo ‘4’.

    The documentation is vague, on WHAT precisely the valid «NLS» language strings are that are valid for «mbstring.language».

    According to http://php.net/manual/en/function.mb-language.php the values are «Japanese», «ja», «English», «en», or «uni» for UTF-8.
    On the other hand, the sample on this current page omits «uni» but introduces «Neutral» as an undocumented option — which is also the default value:

    var_dump ( mb_language () ); // «neutral» (default if not set)
    var_dump ( mb_language ( ‘uni’ ) ); // TRUE, valid language string
    var_dump ( mb_language () ); // «uni»
    var_dump ( mb_language ( ‘neutral’ ) ); // TRUE, valid language string
    var_dump ( mb_language () ); // «neutral»
    ?>

    Note that you should better at least set «mbstring.internal_encoding».

    echo mb_internal_encoding () . ‘
    ‘ ;
    echo mb_regex_encoding ();

    ?>

    You might be surprised at unexpected values.

    mbstring.language Japanese
    ;mbstring.internal_encoding (commented out showing «no value» in phpinfo() )

    These two lines in «php.ini» are the same values as

    «mbstring.internal_encoding» defines the default encoding for «mb_» Functions such as «mb_strlen()».

    It also defines the same for «mb_ereg_» Functions such as «mb_ereg()» when you don’t set «mb_regex_encoding».

    Источник

Читайте также:  Html вставить документ word в
Оцените статью