Text::slugify()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Text
5
 *
6
 * A module of string related utility.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.com
11
 */
12
13
class Text {
14
  use Module;
15
16
  /**
17
   * Fast string templating.
18
   * Uses a Twig-like syntax.
19
   *
20
   * @example
21
   *    echo Text::render('Your IP is : {{ server.REMOTE_HOST }}',array('server' => $_SERVER));
22
   *
23
   * @author Stefano Azzolini <[email protected]>
24
   * @access public
25
   * @static
26
   * @param mixed $t  The text template
27
   * @param mixed $v (default: null)  The array of values exposed in template.
28
   * @return string
29
  */
30
  public static function render($t,$v=null){
31
    if (Options::get('core.text.replace_empties', true)) {
32
      $replacer = function($c) use ($v){
33
        return Structure::fetch(trim($c[1]), $v);
34
      };
35
    } else {
36
      $replacer = function($c) use ($v){
37
        return Structure::fetch(trim($c[1]), $v) ?: $c[0];
38
      };
39
    }
40
41
    return preg_replace_callback("(\{\{([^}]+)\}\})S",$replacer,$t);
42
  }
43
44
  /**
45
   * Create a "slug", an url-safe sanitized string.
46
   *
47
   * @example
48
   *   echo Text::slugify("Thîs îs --- à vêry wrong séntènce!");
49
   *   // this-is-a-very-wrong-sentence
50
   *
51
   * @access public
52
   * @static
53
   * @param  string $text The text to slugify
54
   * @return string       The slug.
55
   */
56
  public static function slugify($text){
57
    return preg_replace(
58
      ['(\s+)','([^a-z0-9-])i','(-+)'],['-','','-'],
59
      strtolower(self::removeAccents($text)));
60
  }
61
62
  /**
63
   * Translit accented characters to neutral ones
64
   *
65
   * @example
66
   *   echo Text::removeAccents("Thîs îs à vêry wrong séntènce!");
67
   *   // This is a very wrong sentence!
68
   *
69
   * @access public
70
   * @static
71
   * @param  string $text The text to translit
72
   * @return string       The translited text
73
   */
74
  public static function removeAccents($text){
75
    static $diac;
76
    return strtr(
77
      utf8_decode($text),
78
      $diac ? $diac : $diac = utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'),
79
      'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
80
  }
81
82
  /**
83
   * Cut a string from the end of a substring to the start of another
84
   *
85
   * @example
86
   *   echo strcut("Name: Ethan Hunt; Role: Agent",'Name: ',';');
87
   *   // Ethan Hunt
88
   *
89
   * @param  string $text      The source text
90
   * @param  string $start_tag The starting substring
91
   * @param  string $end_tag   Ending substring, if omitted all remaining string is returned
92
   * @return string            The cutted string
93
   */
94
  public static function cut($text, $start_tag, $end_tag=null){
95
    $_s = strlen($start_tag) + strpos($text, $start_tag);
96
    return $end_tag ? substr($text, $_s, strpos($text,$end_tag,$_s)-$_s) : substr($text, $_s);
97
  }
98
99
} /* End of class */
100