如何使编码未知的字节序列可用作PHP的输入? [英] How can a byte sequence with unknown coding be made available as input for PHP?

查看:45
本文介绍了如何使编码未知的字节序列可用作PHP的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编码未知的文件.我可以使用file_get_contents()以字符串形式读取此文件.我想导出此字符串,以便可以将其作为PHP代码使用.该字符串可以使用bin2hex()以十六进制表示.但是,如果没有特殊代码,则不能在PHP中使用它.所以我的问题是:如何用PHP输出编码未知的文件中的字节序列,使输出可用作可重现的PHP代码?

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?

推荐答案

使用此功能,可以回显包含任何字符(控制字符..)的字符串.可以复制此字符串并将其作为字符串插入编辑器.

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)) : '';
}

示例:

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

复制输出并用作字符串

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

$ input与$ str相同.带有此功能的输出可以在浏览器中完成,并使用Ctrl C复制.PHP函数(例如var_dump()和var_export())在某些地方会失败.

$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.

示例:

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

在这里浏览器中var_export()的输出失败,因为$ str不是有效的UTF-8.strhex()的输出为\ xe2 \ x82

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

这篇关于如何使编码未知的字节序列可用作PHP的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆