Blame view

src/lib/UTMParser.php 2.21 KB
ef60cd4d   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  <?php
  
  namespace
  {
  
      class UTMParser
      {
          public $url;
          public $utm_source;
          public $utm_medium;
          public $utm_term;
          public $utm_content;
          public $utm_campaign;
  
  
          public function replaceData($target, $replacement, $str)
          {
              $text = str_replace($target, $replacement, $str);
              return $text;
          }
  
          public function getUtm($data, $i)
          {  $utm = '';
  
              if(!empty($data['utm_source'])){
                  $utm = "utm_source=".$data['utm_source'];
              }
  
              if(!empty($data['utm_medium'])) {
                  $utm .= "&utm_medium=".$data['utm_medium'];
              }
  
              if(!empty($i)) {
                  $utm .= "&utm_term=".$i;
              }
  
              if(!empty($data['utm_content'])) {
                  $utm .= "&utm_content=".$data['utm_content'];
              }
  
              if(!empty($data['utm_campaign'])) {
                  $utm .= "&utm_campaign=".$data['utm_campaign'];
              }
              return $utm;
          }
  
          public function getsUtm($i)
          {  $utm = '';
  
  
                  $utm = "utm_source=inform_email";
  
  
                  $utm .= "&utm_medium=email";
  
  
  
                  $utm .= "&utm_term=".$i;
  
  
  
  
  
                  $utm .= "&utm_campaign=vesna_2015";
  
              return $utm;
          }
  
          public function parse($data, $template)
          {
  
              preg_match_all( "/href[\s]*=[\s]*[\"\'](?!.*(?:\.ico|\.css|\?utm))(.[^\"\']*)[\"\']/i", $template['text'], $match,PREG_SET_ORDER );
              $i = 1;
              foreach($match as $row ) {
                  $target = $row[0];
                  $utm = $this->getUtm($data, $i++);
                  //$utm = $this->getыUtm( $i++);
                  $replacement = "href = '$row[1]?$utm'";
                  $template['text'] = $this->replaceData($target, $replacement, $template['text']);
              }
  
              return $template['text'];
          }
  
          public function getItems($data){
              foreach($data as $k => $v){
                  $num = count($v);
                  for($i = 0; $i< $num; $i++){
                      $result[$i][$k] = $v[$i];
                  }
              }
              return $result;
  
          }
  
  
  
  
  
      }
  }