如何在 php 中将文件中的所有字符转换为 ascii 数字


How to convert all the characters in a file to ascii numbers in php

我想在 php 中将文件中的所有字符隐藏为 ASCII 代码?我知道 ord 函数,但是是否有任何函数可以对整个文件起作用?

iconv可以完成这项工作

http://php.net/manual/de/function.iconv.php

它将字符串中指定字符集的字符转换为另一个字符集。 查看无法 1:1 转换的字符的//TRANSLIT 和//IGNORE 特殊值。

要获取字符串中的文件,您可以使用file_get_contents并在 iconv 等与 file_put_contents 一起应用后保存它。

$inputFile = fopen("input.txt", "rb");
$outputFile = fopen("output.txt", "w+");
while (!feof($inputFile)) {
    $inputBlock = fread($inputFile, 8192);
    $outputBlock = '';
    $inputLength = strlen($inputBlock);
    for ($i = 0; $i < $inputLength; ++$i) {
        $outputBlock .= str_pad(dechex(ord($inputBlock{$i})),2,'0',STR_PAD_LEFT);
    }
    fwrite($outputFile,$outputBlock);
}
fclose($inputFile);
fclose($outputFile);