|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-06-18 16:26 UTC] qdinar at gmail dot com
Description: ------------ i use one-byte encoding for script's calculations, and want to provide one-byte strings, but i want to write php code itself in utf-8 because i use non-windows-1251 characters in comments. i have tried "declare(encoding='utf-8');" for that but seems it does not work that way, see example below. i tried that because it is written at http://php.net/manual/en/ini.core.php#ini.zend.script-encoding : " Literal strings will be transliterated from zend.script_enconding to mbstring.internal_encoding, as if mb_convert_encoding() would have been called. " and i thought that maybe " declare(encoding='utf-8'); " sets same "zend.script_enconding" but just for a separate file. (they are somehow related, as i have seen from docs). i do not want to have set it to all files. Test script: --------------- php script file in utf-8 encoding: <?php declare(encoding='utf-8'); mb_internal_encoding('windows-1251'); file_put_contents( 'declare_encoding.txt' , "сэлэм"); ?> Expected result: ---------------- "сэлэм" written with windows-1251 Actual result: -------------- "сэлэм" written with utf-8 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 02:00:01 2025 UTC |
The string is converted at compile time. That means mbstring.internal_encoding has to be set to the desired encoding before the file is even loaded. <?php // first.php mb_internal_encoding('windows-1251'); // or set in php.ini include 'second.php'; ?> <?php // second.php declare(encoding='utf-8'); file_put_contents('declare_encoding.txt', 'сэлэм'); ?>I did, but what I posted is not technically what I tried and I then made a couple assumptions that weren't true. I'm working on creating a new bug report about problems in file conversion with regards to mbstring.internal_encoding, mb_internal_encoding(), default_charset, and internal_encoding, however I'm still trying to understand exactly what does and does not work (and why) and it's taking longer than I thought. I considered this bug report to be about how to use zend.multibyte correctly, and after looking through the documentation I see it talks about the mbstring.internal_encoding setting - not mb_internal_encoding(). Including what I said before about changing the encoding before the file is parsed, this revised code <?php // first.php @ini_set('mbstring.internal_encoding', 'windows-1251'); include 'second.php'; ?> <?php // second.php saved in UTF-8 declare(encoding = 'utf-8'); file_put_contents('declare_encoding.txt', 'сэлэм'); ?> should - and does - work correctly in PHP 7. (It should work in PHP 5.6 too but that series is no longer in active support.)>@ini_set('mbstring.internal_encoding', 'windows-1251'); it works in php 5.6.30. thank you.