xref: /plugin/combo/ComboStrap/StringUtility.php (revision 37748cd8654635afbeca80942126742f0f4cc346)
1*37748cd8SNickeau<?php
2*37748cd8SNickeau
3*37748cd8SNickeaunamespace ComboStrap;
4*37748cd8SNickeau
5*37748cd8SNickeau/**
6*37748cd8SNickeau * Class StringUtility
7*37748cd8SNickeau * @package ComboStrap
8*37748cd8SNickeau * A class with string utility
9*37748cd8SNickeau */
10*37748cd8SNickeauclass StringUtility
11*37748cd8SNickeau{
12*37748cd8SNickeau
13*37748cd8SNickeau
14*37748cd8SNickeau    /**
15*37748cd8SNickeau     * Generate a text with a max length of $length
16*37748cd8SNickeau     * and add ... if above
17*37748cd8SNickeau     * @param $myString
18*37748cd8SNickeau     * @param $length
19*37748cd8SNickeau     * @return string
20*37748cd8SNickeau     */
21*37748cd8SNickeau    static function truncateString($myString, $length)
22*37748cd8SNickeau    {
23*37748cd8SNickeau
24*37748cd8SNickeau        if (strlen($myString) > $length) {
25*37748cd8SNickeau            $suffix = ' ...';
26*37748cd8SNickeau            $myString = substr($myString, 0, ($length - 1) - strlen($suffix)) . $suffix;
27*37748cd8SNickeau        }
28*37748cd8SNickeau        return $myString;
29*37748cd8SNickeau    }
30*37748cd8SNickeau
31*37748cd8SNickeau    /**
32*37748cd8SNickeau     * @param $string
33*37748cd8SNickeau     * @return string - the string without any carriage return
34*37748cd8SNickeau     * Used to compare string without worrying about carriage return
35*37748cd8SNickeau     */
36*37748cd8SNickeau    public static function normalized($string)
37*37748cd8SNickeau    {
38*37748cd8SNickeau        return str_replace("\n", "", $string);
39*37748cd8SNickeau    }
40*37748cd8SNickeau
41*37748cd8SNickeau    /**
42*37748cd8SNickeau     * @param $needle
43*37748cd8SNickeau     * @param $haystack
44*37748cd8SNickeau     * @return bool
45*37748cd8SNickeau     */
46*37748cd8SNickeau    public static function contain($needle, $haystack)
47*37748cd8SNickeau    {
48*37748cd8SNickeau        $pos = strpos($haystack, $needle);
49*37748cd8SNickeau        if ($pos === FALSE) {
50*37748cd8SNickeau            return false;
51*37748cd8SNickeau        } else {
52*37748cd8SNickeau            return true;
53*37748cd8SNickeau        }
54*37748cd8SNickeau    }
55*37748cd8SNickeau
56*37748cd8SNickeau    public static function toString($value)
57*37748cd8SNickeau    {
58*37748cd8SNickeau        /**
59*37748cd8SNickeau         * No transformation if it's a string
60*37748cd8SNickeau         * var_export below is not idempotent
61*37748cd8SNickeau         * ie \ would become \\
62*37748cd8SNickeau         */
63*37748cd8SNickeau        if(is_string($value)){
64*37748cd8SNickeau            return $value;
65*37748cd8SNickeau        }
66*37748cd8SNickeau
67*37748cd8SNickeau        $string = var_export($value, true);
68*37748cd8SNickeau
69*37748cd8SNickeau        // An array value gets command in var_export
70*37748cd8SNickeau        $lastCharacterIndex = strlen($string) - 1;
71*37748cd8SNickeau        if ($string[0] === "'" && $string[$lastCharacterIndex] === "'") {
72*37748cd8SNickeau            $string = substr($string, 1, strlen($string) - 2);
73*37748cd8SNickeau        }
74*37748cd8SNickeau        return $string;
75*37748cd8SNickeau
76*37748cd8SNickeau    }
77*37748cd8SNickeau
78*37748cd8SNickeau    /**
79*37748cd8SNickeau     * Add an EOL if not present at the end of the string
80*37748cd8SNickeau     * @param $doc
81*37748cd8SNickeau     */
82*37748cd8SNickeau    public static function addEolCharacterIfNotPresent(&$doc)
83*37748cd8SNickeau    {
84*37748cd8SNickeau        if ($doc[strlen($doc) - 1] != DOKU_LF) {
85*37748cd8SNickeau            $doc .= DOKU_LF;
86*37748cd8SNickeau        }
87*37748cd8SNickeau    }
88*37748cd8SNickeau
89*37748cd8SNickeau    /**
90*37748cd8SNickeau     * Delete the string from the end
91*37748cd8SNickeau     * This is used generally to delete the previous opening tag of an header or a blockquote
92*37748cd8SNickeau     * @param $doc
93*37748cd8SNickeau     * @param $string
94*37748cd8SNickeau     */
95*37748cd8SNickeau    public static function rtrim(&$doc, $string)
96*37748cd8SNickeau    {
97*37748cd8SNickeau
98*37748cd8SNickeau        /**
99*37748cd8SNickeau         * We trim because in the process, we may get extra {@link DOKU_LF} at the end
100*37748cd8SNickeau         */
101*37748cd8SNickeau        $doc = trim($doc);
102*37748cd8SNickeau        $string = trim($string);
103*37748cd8SNickeau        $length = strlen($doc) - strlen($string);
104*37748cd8SNickeau        if (substr($doc, $length) === $string) {
105*37748cd8SNickeau            $doc = substr($doc, 0, $length);
106*37748cd8SNickeau        }
107*37748cd8SNickeau
108*37748cd8SNickeau    }
109*37748cd8SNickeau
110*37748cd8SNickeau    /**
111*37748cd8SNickeau     * Delete the string from the beginning
112*37748cd8SNickeau     * This is used to delete a tag for instance
113*37748cd8SNickeau     * @param $doc
114*37748cd8SNickeau     * @param $string
115*37748cd8SNickeau     */
116*37748cd8SNickeau    public static function ltrim(&$doc, $string)
117*37748cd8SNickeau    {
118*37748cd8SNickeau
119*37748cd8SNickeau        $doc = trim($doc);
120*37748cd8SNickeau        $string = trim($string);
121*37748cd8SNickeau        $length = strlen($string);
122*37748cd8SNickeau        if (substr($doc, 0, $length) === $string) {
123*37748cd8SNickeau            $doc = substr($doc, $length);
124*37748cd8SNickeau        }
125*37748cd8SNickeau
126*37748cd8SNickeau    }
127*37748cd8SNickeau
128*37748cd8SNickeau    /**
129*37748cd8SNickeau     * The word count does not take into account
130*37748cd8SNickeau     * words with non-words characters such as < =
131*37748cd8SNickeau     * Therefore the node <node> and attribute name=value are not taken in the count
132*37748cd8SNickeau     * @param $text
133*37748cd8SNickeau     * @return int the number of words
134*37748cd8SNickeau     */
135*37748cd8SNickeau    public static function getWordCount($text)
136*37748cd8SNickeau    {
137*37748cd8SNickeau        /**
138*37748cd8SNickeau         * Delete the frontmatter
139*37748cd8SNickeau         */
140*37748cd8SNickeau        $text = preg_replace("/^---(json)?$.*^---$/Ums", "", $text);
141*37748cd8SNickeau        /**
142*37748cd8SNickeau         * New line for node
143*37748cd8SNickeau         */
144*37748cd8SNickeau        $text = str_replace("<", "\n<", $text);
145*37748cd8SNickeau        $text = str_replace(">", ">\n", $text);
146*37748cd8SNickeau        // \s shorthand for whitespace
147*37748cd8SNickeau        // | the table and links are separated with a |
148*37748cd8SNickeau        // / to take into account expression such as and/or
149*37748cd8SNickeau        // /u for unicode support (https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php)
150*37748cd8SNickeau        $wordSeparator = '/[\s|\/]/u';
151*37748cd8SNickeau        $preg_split = preg_split($wordSeparator, $text);
152*37748cd8SNickeau        $wordsWithoutEmpty = array_filter($preg_split, 'self::isWord');
153*37748cd8SNickeau        return count($wordsWithoutEmpty);
154*37748cd8SNickeau    }
155*37748cd8SNickeau
156*37748cd8SNickeau    public static function normalize($expected)
157*37748cd8SNickeau    {
158*37748cd8SNickeau        $expected = preg_replace("/[\s]/", " ", $expected);
159*37748cd8SNickeau        $expected = str_replace("  ", " ", $expected);
160*37748cd8SNickeau        $expected = str_replace("  ", " ", $expected);
161*37748cd8SNickeau        $expected = str_replace("  ", " ", $expected);
162*37748cd8SNickeau        $expected = str_replace("  ", " ", $expected);
163*37748cd8SNickeau        return trim($expected);
164*37748cd8SNickeau
165*37748cd8SNickeau    }
166*37748cd8SNickeau
167*37748cd8SNickeau    /**
168*37748cd8SNickeau     * @param $text
169*37748cd8SNickeau     * @return bool
170*37748cd8SNickeau     */
171*37748cd8SNickeau    public static function isWord($text)
172*37748cd8SNickeau    {
173*37748cd8SNickeau        if (empty($text)) {
174*37748cd8SNickeau            return false;
175*37748cd8SNickeau        }
176*37748cd8SNickeau        /**
177*37748cd8SNickeau         * We also allow `-` minus
178*37748cd8SNickeau         *
179*37748cd8SNickeau         * And because otherwise the words are not counted:
180*37748cd8SNickeau         *   * `'` (used to highlight words)
181*37748cd8SNickeau         *   * `[]` used in links
182*37748cd8SNickeau         *   * `,` used at the end of a sentenct
183*37748cd8SNickeau         */
184*37748cd8SNickeau        $preg_match = preg_match("/^[\w\-'\]\[,]*$/u", $text);
185*37748cd8SNickeau        return $preg_match == 1;
186*37748cd8SNickeau    }
187*37748cd8SNickeau
188*37748cd8SNickeau    public static function match($subject, $pattern)
189*37748cd8SNickeau    {
190*37748cd8SNickeau        return preg_match("/$pattern/", $subject) === 1;
191*37748cd8SNickeau    }
192*37748cd8SNickeau
193*37748cd8SNickeau    public static function endWiths($string, $suffix)
194*37748cd8SNickeau    {
195*37748cd8SNickeau        $suffixStartPosition = strlen($string) - strlen($suffix);
196*37748cd8SNickeau        return strrpos($string, $suffix) === $suffixStartPosition;
197*37748cd8SNickeau    }
198*37748cd8SNickeau
199*37748cd8SNickeau    public static function explodeAndTrim($string, $delimiter = ",")
200*37748cd8SNickeau    {
201*37748cd8SNickeau        return array_map('trim', explode($delimiter, $string));
202*37748cd8SNickeau    }
203*37748cd8SNickeau
204*37748cd8SNickeau    public static function lastIndexOf($haystack, $needle)
205*37748cd8SNickeau    {
206*37748cd8SNickeau        /**
207*37748cd8SNickeau         * strRpos
208*37748cd8SNickeau         * and not strpos
209*37748cd8SNickeau         */
210*37748cd8SNickeau        return strrpos($haystack, $needle);
211*37748cd8SNickeau    }
212*37748cd8SNickeau
213*37748cd8SNickeau    public static function startWiths($string, $prefix)
214*37748cd8SNickeau    {
215*37748cd8SNickeau        return strrpos($string, $prefix) === 0;
216*37748cd8SNickeau    }
217*37748cd8SNickeau
218*37748cd8SNickeau}
219