开发者

Read plain text from binary file with PHP

开发者 https://www.devze.com 2022-12-22 02:25 出处:网络
File 1: asdffdsa File 2: asdfjklfdsaHGUik

File 1:

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

File 2:

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

How do I read these binary files with PHP such that I can populate an array with the plaintext like:

$file1_output = ["asdf", "fdsa"];
$file2_output = ["asdfjkl", "fdsaHGUik"];
开发者_高级运维


This will match any word character (0-9,a-z,A-Z and _):

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


Not sure if you could do this some better way, but maybe reading char-by-char from file and checking if it's ASCII code (using ord() function) is in range you're interested in - would also do the trick?


To build on what @Frank Farmer said, I'd use 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);

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消