MiniMVC /lib/MiniMVC/MiniMVC/Helper/Text.php

Language PHP Lines 77
MD5 Hash 98f319edf666bfb4fcd38a3ec3ac2e6c
Repository https://github.com/tquensen/MiniMVC.git View Raw File View Project SPDX
 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
<?php

class Helper_Text extends MiniMVC_Helper
{

    public function sanitize($input, $lower = false, $maxlength = false)
    {
        $list = $this->getReplaceList();
        $output = str_replace(array_keys($list), array_values($list), $input);
        $output = preg_replace('/[^A-Za-z0-9_-]+/', '-', $output);
        if ($maxlength) {
            $output = mb_substr($output, 0, $maxlength);
        }
        return $lower ? strtolower($output) : $output;
    }

    public function getReplaceList()
    {
        $replace = array(
            'Ü' => 'Ue',
            'Ö' => 'Oe',
            'Ä' => 'Ae',
            'ü' => 'ue',
            'ö' => 'oe',
            'ä' => 'ae',
            'ß' => 'ss',
            '@' => '-at-'
        );

        return $replace;
    }

    public function truncate($content, $maxLength, $cutSize = 50,
            $insertAfter = '...')
    {
        if (mb_strlen($content, 'UTF-8') <= $maxLength) {
            return $content;
        }
        $content = mb_substr($content, 0, $maxLength);
        //try to cut after a sentence
        $LastCharSentence = array();
        $lastCharSentence['questionmark'] = mb_strrpos($content, '? ');
        $lastCharSentence['exclamationmark'] = mb_strrpos($content, '! ');
        $lastCharSentence['period'] = mb_strrpos($content, '. ');
        //or at least after a word
        $lastChar = array();
        $lastChar['space'] = mb_strrpos($content, ' ');
        $lastChar['newline'] = mb_strrpos($content, "\n");

        if (max($lastCharSentence) != 0 && (max($lastCharSentence) + 1) > ($maxLength - $cutSize)) {
            return mb_substr($content, 0, max($lastCharSentence) + 2) . $insertAfter;
        } elseif (max($lastChar) > $maxLength - $cutSize) {
            return mb_substr($content, 0, max($lastChar) + 1) . $insertAfter;
        } else {
            return $content . $insertAfter;
        }
    }

    public function esc($content, $print = true)
    {
        if ($print) {
            echo htmlspecialchars($content);
            return;
        }
        return htmlspecialchars($content);
    }

    public function raw($content, $print = true)
    {
        if ($print) {
            echo html_entity_decode($content, ENT_QUOTES, 'UTF-8');
            return;
        }
        return html_entity_decode($content, ENT_QUOTES, 'UTF-8');
    }

}
Back to Top