Created
May 26, 2015 21:51
-
-
Save andronex/31bd1ef5e3e3e558ebfe to your computer and use it in GitHub Desktop.
Парсер для писем или текстовых файлов.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Парсер для писем или текстовых файлов. | |
* Для записи в базу юзается библиотека http://dklab.ru/lib/DbSimple/manual.html | |
*/ | |
//ini_set('display_errors', 1); | |
//ini_set('error_reporting', -1); | |
require_once "db/lib/config.php"; | |
require_once "db/lib/DbSimple/Generic.php"; | |
//заменить юзера, пароль и имя базы | |
$DB = DbSimple_Generic::connect("mysql://root:***@localhost/db_name"); | |
// Устанавливаем обработчик ошибок. | |
$DB->setErrorHandler('databaseErrorHandler'); | |
// Устанавливаем кодировку | |
mysql_query("SET NAMES cp1251"); | |
// Код обработчика ошибок SQL. | |
function databaseErrorHandler($message, $info) | |
{ | |
// Если использовалась @, ничего не делать. | |
if (!error_reporting()) return; | |
// Выводим подробную информацию об ошибке. | |
echo "SQL Error: $message<br><pre>"; | |
print_r($info); | |
echo "</pre>"; | |
exit(); | |
} | |
//фун-ция для записи в БД распарсенных данных | |
function insertDB ($address, $fio, $mail, $tel, $products, $comment) { | |
global $DB; | |
$res = $DB->query('insert into my_orders (id, address, fio, mail, tel, products, comment) | |
values (?, ?, ?, ?, ?, ?, ?) | |
', null, $address, $fio, $mail, $tel, $products, $comment); | |
if($res) return 'ok'; | |
} | |
//парсим 746 файлов | |
for ($b = 1; $b <= 746; $b++) { | |
$a = file(dirname(__FILE__).'/contacts/'.$b.'.eml'); | |
$out = $address = $fio = $mail = $tel = $prod = $tovars = $comment = ''; | |
foreach($a as $i => $line) { | |
if(strstr($line, iconv("UTF-8", "Windows-1251", "Адрес: "))) { | |
while (!empty(trim($a[$i]))) { | |
$address .= trim(mb_convert_encoding(str_replace(iconv("UTF-8", "Windows-1251", "Адрес: "),'',$a[$i]), 'Windows-1252', "HTML-ENTITIES")).' '; | |
$i++; | |
} | |
} | |
if(strstr($line, iconv("UTF-8", "Windows-1251", "Комментарий: "))) { | |
while (!empty(trim($a[$i]))) { | |
$comment .= trim(mb_convert_encoding(str_replace(iconv("UTF-8", "Windows-1251", " Комментарий: "),'',$a[$i]), 'Windows-1252', "HTML-ENTITIES")).' '; | |
$i++; | |
} | |
} | |
if(strstr($line, iconv("UTF-8", "Windows-1251", "ФИО:"))) { | |
while (!empty(trim($a[$i]))) { | |
$fio .= trim(mb_convert_encoding(str_replace(iconv("UTF-8", "Windows-1251", " ФИО:"),'',$a[$i]), 'Windows-1252', "HTML-ENTITIES")).' '; | |
$i++; | |
} | |
} | |
if(strstr($line, iconv("UTF-8", "Windows-1251", "Мыло: "))) { | |
while (!empty(trim($a[$i]))) { | |
$mail .= trim(mb_convert_encoding(str_replace(iconv("UTF-8", "Windows-1251", " Мыло: "),'',$a[$i]), 'Windows-1252', "HTML-ENTITIES")); | |
$i++; | |
} | |
} | |
if(strstr($line, iconv("UTF-8", "Windows-1251", "Телефон: "))) { | |
while (!empty(trim($a[$i]))) { | |
$tel .= trim(mb_convert_encoding(str_replace(iconv("UTF-8", "Windows-1251", " Телефон: "),'',$a[$i]), 'Windows-1252', "HTML-ENTITIES")).' '; | |
$i++; | |
} | |
} | |
if(strstr($line, iconv("UTF-8", "Windows-1251", "Товар:"))) { | |
while (!empty(trim($a[$i]))) { | |
$product = str_replace(iconv("UTF-8", "Windows-1251", " Товар: "),'',$a[$i]); | |
preg_match("/\[product_id\]\=[0-9]{1,10}/", $product, $products); | |
if(is_array($products)) { | |
foreach($products as $prod) { | |
$tovars .= str_replace('[product_id]=','',$prod).';'; | |
} | |
} | |
else $tovars .= $products.';'; | |
$i++; | |
} | |
} | |
else continue; | |
} | |
echo insertDB($address, $fio, $mail, $tel, $tovars, $comment); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment