Skip to content

Instantly share code, notes, and snippets.

@LazyDay
Created May 21, 2018 17:34
Show Gist options
  • Save LazyDay/994882c920d1a180296fa798deb73a2c to your computer and use it in GitHub Desktop.
Save LazyDay/994882c920d1a180296fa798deb73a2c to your computer and use it in GitHub Desktop.
<?php
class DataParser{
public $data;
public function __construct($data){
$this->data = $data;
}
public function parse(){
$this->data = explode(' ', preg_replace('|[\r\n]+|', ' ', $this->data));
$i = 0;
while($i < count($this->data) - 1){
$s[] = mb_strtolower($this->data[$i]." ".$this->data[$i+1]);
$i++;
}
$this->data = $s;
$wallet = $this->parseWallet();
$code = $this->parseCode();
$sum = $this->parseSum();
return new Data($code, $sum, $wallet);
}
protected function parseSum(){
$dict1 = ['сум', 'спис', 'спиш', 'спи', 'опл'];
$dict2 = ['р.', 'рублей', 'руб', 'р '];
$result = [];
for($i = 0; $i < count($this->data);$i++){
$r = false;
foreach ($dict1 as $value) {
if(preg_match("/(".$value.")/", $this->data[$i])){
$r = true;
}
}
if($r){
foreach ($dict2 as $value) {
if(preg_match("/(".$value.")/", $this->data[$i])){
preg_match("/\d+[,|.]\d+/",$this->data[$i],$d);
$result[] = $d[0];
}
}
}
}
$result = array_values(array_unique($result));
return $result[0];
}
protected function parseCode(){
$dict = ['код', 'арол', 'од'];
$result = [];
for($i = 0; $i < count($this->data);$i++){
$r = false;
foreach ($dict as $value) {
if(preg_match("/(".$value.")/", $this->data[$i])){
$r = true;
}
}
if($r){
preg_match('/\d{3,6}/', $this->data[$i], $v);
if(!empty($v[0])){
$result[] = $v[0];
unset($this->data[$i]);
}
}
}
$this->data = array_values($this->data);
$result = array_values(array_unique($result));
return $result[0];
}
protected function parseWallet(){
$dict = ['сче','счё','кош','ном'];
$result = [];
for($i = 0; $i < count($this->data);$i++){
$r = false;
foreach ($dict as $value) {
if(preg_match("/(".$value.")/", $this->data[$i])){
$r = true;
}
}
if($r){
preg_match('/\d{8,16}/', $this->data[$i], $v);
if(!empty($v[0])){
$result[] = $v[0];
unset($this->data[$i]);
}
}
}
$result = array_values(array_unique($result));
$this->data = array_values($this->data);
return $result[0];
}
}
class Data{
public $code;
public $sum;
public $wallet;
public function __construct($code, $sum, $wallet){
$this->code = $code;
$this->sum = $sum;
$this->wallet = $wallet;
}
}
$data = "Пароль: 9540
Спишется 3136,69р.
Перевод на счет 410011281261237";
$p = new DataParser($data);
print_r($p->parse());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment