Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as PHP by ilya ( 7 years ago )
<?php

$string="
Спишется 50,26р. Пароль 6675
Перевод на счет 41001830685343";
interface IConfirmationSMS {
	public function setConfirmationCode($confirmation_code);
	public function getConfirmationCode();
	
	public function setPurseId($purse_id);
	public function getPurseId();
	
	public function setPaymentAmount($payment_amount);
	public function getPaymentAmount();
}
interface IConfirmationSMSParser {
	public function parse($message, IConfirmationSMS $confirmationSMS);
}
class YMConfirmationSMS implements IConfirmationSMS {
	private $data = [];
	
	public function setConfirmationCode($confirmation_code) {
		$this->data['confirmation_code'] = $confirmation_code;
	}
	
	public function getConfirmationCode() {
		return $this->data['confirmation_code'] ?: null;
	}
	
	public function setPurseId($purse_id) {
		$this->data['purse_id'] = $purse_id;
	}
	
	public function getPurseId() {
		return $this->data['purse_id'] ?: null;
	}
	
	public function setPaymentAmount($payment_amount) {
		$this->data['payment_amount'] = $payment_amount;
	}
	
	public function getPaymentAmount() {
		return $this->data['payment_amount'] ?: null;
	}
}
class YMConfirmationSMSParser implements IConfirmationSMSParser {
	
	public function parse($message, IConfirmationSMS $confirmationSMS) {
		$confirmationSMS->setConfirmationCode($this->extractConfirmationCode($message));
		$confirmationSMS->setPurseId($this->extractPurseId($message));
		$confirmationSMS->setPaymentAmount($this->extractPaymentAmount($message));
		return $confirmationSMS;	
	}
		
	private function extractNumericValues($string) {
		preg_match_all('/((?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?)/', $string, $matches);
		return $matches[0] ?: [];
	}
	
	private function findIntegerByLength($string, $length) {
		$numericValues = $this->extractNumericValues($string);
		foreach($numericValues AS $value) {
			if((int)$value == (float)$value AND strlen($value) == $length) {
				return (int)$value;
			}
		}
		return null;
	}
	
	private function findFloat($string) {
		$numericValues = $this->extractNumericValues($string);
		foreach($numericValues AS $value) {
			$value = str_replace(',', '.', $value);
			if((int)$value != (float)$value) {
				return (float)$value;
			}
		}
		return null;
	}
	
	private function extractConfirmationCode($string) {
		return $this->findIntegerByLength($string, 4);
	}
	private function extractPurseId($string) { 
		return $this->findIntegerByLength($string, 14);
	}
	
	private function extractPaymentAmount($string) {
		return $this->findFloat($string);
	}
}
$confirmationSMSParser = new YMConfirmationSMSParser();
$confirmationSMS = $confirmationSMSParser->parse($string, new YMConfirmationSMS());
echo 'code: '.$confirmationSMS->getConfirmationCode().'<br/>';
echo 'purse: '.$confirmationSMS->getPurseId().'<br/>';
echo 'amount: '.$confirmationSMS->getPaymentAmount().'<br/>';

?>

 

Revise this Paste

Your Name: Code Language: