How can a byte sequence with unknown coding be made available as input for PHP?

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-15 00:11:54

问题


I have a file with an unknown encoding. I can read this file as a string with file_get_contents(). I would like to export this string so that it can be made available as PHP code. The string can be represented in hexadecimal using bin2hex (). However, this cannot be used in PHP without special code. So my question: How can a byte sequence from a file with unknown encoding be output with PHP in such a way that the output can be used as reproducible PHP code?


回答1:


var_export is probably the function you are looking for. jspit's solution also works, but var_export is going to be more compact.




回答2:


With this function, a string with any characters (control characters ..) can be echoed. This character string can be copied and inserted as a string in the editor.

function strhex($s){
    return $s != '' ? '\\x'.implode('\\x',str_split(bin2hex($s),2)) : '';
}

example:

$str = "íéťů4€ ";  //"my unknown string"
echo strhex($str);
//\xc3\xad\xc3\xa9\xc5\xa5\xc5\xaf\x34\xe2\x82\xac\x20\x01

Copy the output and use as string

$input = "\xc3\xad\xc3\xa9\xc5\xa5\xc5\xaf\x34\xe2\x82\xac\x20\x01";

$input is identical to $str. The output with this function can be done in the browser and copied with Ctrl C. PHP functions like var_dump() and var_export() fail in several places.

Example:

$str = "\xe2\x82";
var_export($str);
//'�'

The output of var_export() in the browser fails here because $str is not a valid UTF-8. The output with strhex() is \xe2\x82



来源:https://stackoverflow.com/questions/65028433/how-can-a-byte-sequence-with-unknown-coding-be-made-available-as-input-for-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!