Skip to content

Instantly share code, notes, and snippets.

@caiyili
Last active April 15, 2018 15:53
Show Gist options
  • Save caiyili/8afa6da9059fadd3f1380bcab9dc9caf to your computer and use it in GitHub Desktop.
Save caiyili/8afa6da9059fadd3f1380bcab9dc9caf to your computer and use it in GitHub Desktop.
<?php
$base_dir = $argv[1];
$rs_dir = opendir($base_dir);
while ($filename = readdir($rs_dir)) {
if (false === strpos($filename, '.doc')) {
continue;
}
$filepath = $base_dir . '/' . $filename;
$arr_line = file($filepath);
$boundary = '';
$arr_part = [];
$part = '';
$part_is_text = 0;
foreach ($arr_line as $line) {
$line = trim($line);
if (preg_match('/^Content-Type:multipart.*boundary="([a-z0-9A-Z_-]+)"$/', $line, $m)) {
$boundary = $m[1];
}
if ($boundary) {
if (false !== strpos($line, $boundary)) {
if ($part) {
$arr_part[] = $part;
}
$part = '';
$part_is_text = 0;
} elseif (preg_match('/^Content-Type:text.*/', $line)) {
$part_is_text = 1;
} elseif (preg_match('/:/', $line)) {
//is header
} elseif ($line) {
if ($part_is_text) {
$part .= trim($line);
}
}
}
}
$arr_mobile = [];
foreach ($arr_part as $part) {
$utf8_content = iconv('GBK', 'UTF8', base64_decode($part));
if (preg_match('/1[0-9]{10}/', $utf8_content, $m)) {
$arr_mobile[] = $m[0];
}
}
$mobile = join(',', $arr_mobile);
if (preg_match('/.*_(.+)-(.+)\.doc/', $filename, $m)) {
$name = $m[1];
$city = $m[2];
printf("%s\t%s\t%s\n", $name, $city, $mobile);
} else {
echo "==={$filename}====不合法\n";
}
}
closedir($rs_dir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment