使用PHP从二进制文件中读取纯文本

时间:2010-03-04 15:14:57

标签: php parsing binary

文件1:

asdffdsa

文件2:

asdfjklfdsaHGUik

如何使用PHP读取这些二进制文件,以便我可以使用明文填充数组:

$file1_output = ["asdf", "fdsa"];
$file2_output = ["asdfjkl", "fdsaHGUik"];

3 个答案:

答案 0 :(得分:1)

这将匹配任何单词字符(0-9,a-z,A-Z和_):

preg_match_all(
    "/[\x30-\x39\x5F\x41-\x5A\x61-\x7a]+/", /* regexp */
    file_get_contents('file1'),             /* file contents */
    $file1_output                           /* array to populate */
);

答案 1 :(得分:0)

不确定你是否可以更好地做到这一点,但也许从文件中读取char-by-char并检查它是否是ASCII代码(使用ord()函数)在你感兴趣的范围内 - 也会这样做欺骗?

答案 2 :(得分:0)

在@Frank Farmer所说的基础上,我会使用strings

<?php

$strings_command = '/usr/bin/strings';

$file1_output = array();
$file2_output = array();

exec("$strings_command $path_to_file1",$file1_output);
exec("$strings_command $path_to_file2",$file2_output);

?>
相关问题