Blame view

src/lib/UTMParser.php 1.66 KB
1ea3b987   Administrator   maby 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
  <?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 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++);

                  $replacement = "href = '$row[1]?$utm'";

                  $template['text'] = $this->replaceData($target, $replacement, $template['text']);

              }

  

              return $template['text'];

          }

  

  

      }

  }