Read plain text from binary file with PHP
File 1:
asdffdsa
File 2:
asdfjklfdsaHGUik
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);
?>
精彩评论