Completed
Push — master ( b97b25...127e50 )
by Mikael
02:17
created

TTextUtilities::wrapElementWithStartEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
cc 1
eloc 6
nc 1
nop 5
crap 2
1
<?php
2
3
namespace Mos\TextFilter;
4
5
/**
6
 * Utilities when working with text.
7
 *
8
 */
9
trait TTextUtilities
10
{
11
    /**
12
     * Get text until <!--stop--> or all text.
13
     *
14
     * @param string $text with content
15
     *
16
     * @return string with text
17
     */
18 8
    public function getUntilStop($text)
19
    {
20 8
        $pos = stripos($text, "<!--stop-->");
21 8
        if ($pos) {
22 2
            $text = substr($text, 0, $pos);
23 2
        }
24 8
        return $text;
25
    }
26
27
28
29
    /**
30
     * Get text until <!--more--> or all text.
31
     *
32
     * @param string $text with content
33
     *
34
     * @return array with text and boolean if more was detected.
35
     */
36 8
    public function getUntilMore($text)
37
    {
38 8
        $pos = stripos($text, "<!--more-->");
39 8
        $hasMore = $pos;
40 8
        if ($pos) {
41 1
            $text = substr($text, 0, $pos);
42 1
        }
43 8
        return [$text, $hasMore];
44
    }
45
46
47
48
    /**
49
     * Wrap HTML element with with start and end.
50
     *
51
     * @param string  $text  with content
52
     * @param string  $tag   HTML tag to search for
53
     * @param string  $start wrap start part
54
     * @param string  $end   wrap end part
55
     * @param number  $count hits to search for
56
     *
57
     * @return array with text and boolean if more was detected.
58
     */
59
    public function wrapElementWithStartEnd($text, $tag, $start, $end, $count)
60
    {
61
        return preg_replace(
62
            "#(<$tag>)(.*?)(</$tag>)#",
63
            "$start$1$2$3$end</a>",
64
            $text,
65
            $count
66
        );
67
    }
68
69
70
71
    /**
72
    * Wrap content of a HTML element with start and end.
73
     *
74
     * @param string  $text  with content
75
     * @param string  $tag   HTML tag to search for
76
     * @param string  $start wrap start part
77
     * @param string  $end   wrap end part
78
     * @param number  $count hits to search for
79
     *
80
     * @return array with text and boolean if more was detected.
81
     */
82
    public function wrapElementContentWithStartEnd($text, $tag, $start, $end, $count)
83
    {
84
        return preg_replace(
85
            "#(<$tag>)(.*?)(</$tag>)#",
86
            "$1$start$2$end$3</a>",
87
            $text,
88
            $count
89
        );
90
    }
91
92
93
94
95
    /**
96
     * Returns the excerpt of the text with at most the specified amount of characters.
97
     *
98
     * @param int $chars the number of characters to return.
99
     * @param boolean $hard do a hard break at exactly $chars characters or find closest space.
100
     * @return string as the excerpt.
101
     */
102
/*    public function GetExcerpt($chars=139, $hard=false) {
103
      if(!isset($this->data['data_filtered'])) {
104
        return null;
105
      }
106
      $excerpt = strip_tags($this->data['data_filtered']);
107
108
      if(strlen($excerpt) > $chars) {
109
        $excerpt   = substr($excerpt, 0, $chars-1);
110
      }
111
112
      if(!$hard) {
113
        $lastSpace = strrpos($excerpt, ' ');
114
        $excerpt   = substr($excerpt, 0, $lastSpace);
115
      }
116
117
      return $excerpt;
118
    }
119
    
120
    
121
    /**
122
     * Returns the first paragraph ot the text.
123
     * 
124
     * @return string as the first paragraph.
125
     */
126
/*    public function GetFirstParagraph() {
127
      if(!isset($this->data['data_filtered'])) {
128
        return null;
129
      }
130
      $excerpt = $this->data['data_filtered'];
131
132
      $firstPara = strpos($excerpt, '</p>');
133
      $excerpt   = substr($excerpt, 0, $firstPara + 4);
134
135
      return $excerpt;
136
    }
137
*/
138
}
139