fork download
  1. <?php
  2.  
  3. class Tyre {
  4. /**
  5.   * RegExp
  6.   *
  7.   * @var string
  8.   */
  9. private $pattern;
  10.  
  11. /**
  12.   * @var string
  13.   */
  14. private $subject;
  15.  
  16. /**
  17.   * @var string
  18.   */
  19. private $brand;
  20.  
  21. /**
  22.   * @var string
  23.   */
  24. private $width;
  25.  
  26. /**
  27.   * @var string
  28.   */
  29. private $serial;
  30.  
  31. /**
  32.   * @var string
  33.   */
  34. private $radius;
  35.  
  36. /**
  37.   * @var string
  38.   */
  39. private $index;
  40.  
  41. /**
  42.   * @var string
  43.   */
  44. private $model;
  45.  
  46. private $tyreBrands = array(
  47. 'Nokian',
  48. 'Good Year',
  49. 'Continental',
  50. // ...
  51. );
  52.  
  53. public function __construct($subject) {
  54. $this->pattern = '#(' . implode('|', $this->tyreBrands) . ')\s(\d{3})/(\d{2})\sR(\d{2})\s(\d{2,3}\w)\s(.*)#';
  55. $this->subject = $subject;
  56. }
  57.  
  58. public function parse() {
  59. if (preg_match($this->pattern, $this->subject, $matches)) {
  60. if (count($matches) !== 7) {
  61. throw new LogicException('Что-то пошло не так, слишком мало данные после парсинга.');
  62. }
  63. $this->brand = $matches[1];
  64. $this->width = $matches[2];
  65. $this->serial = $matches[3];
  66. $this->radius = $matches[4];
  67. $this->index = $matches[5];
  68. $this->model = $matches[6];
  69. }
  70.  
  71. echo 'Brand: ', $this->brand;
  72. echo '<br>';
  73. echo 'Типоразмер: ', $this->width, '/', $this->serial;
  74. echo '<br>';
  75. echo 'Радиус: ', $this->radius;
  76. echo '<br>';
  77. echo 'Индекс скорости: ', $this->index;
  78. echo '<br>';
  79. echo 'Модель: ', $this->model;
  80. echo '<hr>', PHP_EOL;
  81. }
  82. }
  83.  
  84. $tyre = new Tyre('Nokian 185/70 R14 92T HKPL5');
  85. $tyre->parse();
  86.  
  87. $tyre = new Tyre('Nokian 245/45 R18 100T HKPL 8 Run Flat XL');
  88. $tyre->parse();
  89.  
  90. $tyre = new Tyre('Good Year 175/65 R14 82T UG ICE ARCTIC D-STUD');
  91. $tyre->parse();
  92.  
  93. $tyre = new Tyre('Good Year 205/70 R15 96T UG ICE ARCTIC D-STUD SUV');
  94. $tyre->parse();
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Brand: Nokian<br>Типоразмер: 185/70<br>Радиус: 14<br>Индекс скорости: 92T<br>Модель: HKPL5<hr>
Brand: Nokian<br>Типоразмер: 245/45<br>Радиус: 18<br>Индекс скорости: 100T<br>Модель: HKPL 8 Run Flat  XL<hr>
Brand: Good Year<br>Типоразмер: 175/65<br>Радиус: 14<br>Индекс скорости: 82T<br>Модель: UG ICE ARCTIC D-STUD<hr>
Brand: Good Year<br>Типоразмер: 205/70<br>Радиус: 15<br>Индекс скорости: 96T<br>Модель: UG ICE ARCTIC D-STUD SUV<hr>