user286355
user286355

Reputation:

Read plain text from binary file with PHP

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"];

Upvotes: 0

Views: 934

Answers (3)

Josh
Josh

Reputation: 11070

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);

?>

Upvotes: 0

dev-null-dweller
dev-null-dweller

Reputation: 29462

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 */
);

Upvotes: 1

Nobwyn
Nobwyn

Reputation: 8685

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?

Upvotes: 0

Related Questions