fluxcms /inc/bx/helpers/string.php

Language PHP Lines 440
MD5 Hash 27f15f9943d19340331a9a91254ac6b5
Repository https://github.com/chregu/fluxcms.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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
class bx_helpers_string {

    static function truncate($inStr, $length = 100, $breakWords = false, $etc = '...') {
        if ($length == 0)
            return '';

        if (strlen($inStr) > $length) {
            $length -= strlen($etc);
            if (!$breakWords) {
                $inStr = preg_replace('/\s+?(\S+)?$/', '', substr($inStr, 0, $length + 1));
            }

            return substr($inStr, 0, $length) . " $etc";
        } else
            return $inStr;
    }

    static function truncate_strip($inStr, $length = 100, $breakWords = false, $etc = '...') {
        $inStr = strip_tags($inStr);
        $inStr = bx_helpers_string::truncate($inStr, $length, $breakWords, $etc);
        return $inStr;
    }

    static function nl2property_hegu($text) {
        $text = $text[0]->ownerDocument->saveXML($text[0]);
        $text = preg_replace(array(
                '#<[\/]*meta_other>#',
                "/^([^:\n]*):(.*)$/m"), array(
                '',
                "<name>\$1</name><value>\$2</value>"), $text);
        $text = "<div><p>" . preg_replace(array(
                "#\n#"), array(
                "</p>\n<p>",
                ''), $text) . "</p>\n</div>";
        $xml = new DomDocument();
        $xml->loadXML($text);
        return $xml;
    }

    static function explodeToNode($separator, $string, $childNodeName = 'child', $rootNodeName = 'root') {
        $dom = new DOMDocument();
        $dom->appendChild($dom->createElement($rootNodeName));

        if (!empty($string)) {
            $exploded = explode($separator, $string);
            foreach ($exploded as $element) {
                $child = $dom->createElement($childNodeName);
                $tn = $dom->createTextNode($element);
                $child->appendChild($tn);
                $dom->documentElement->appendChild($child);
            }
        }
        return $dom;
    }

    /**
     * takes a string of utf-8 encoded characters and converts it to a string of unicode entities
     * each unicode entitiy has the form &#nnnnn; n={0..9} and can be displayed by utf-8 supporting
     * browsers
     *
     * from http://ch.php.net/manual/en/function.utf8-decode.php and optimized
     *
     * @param $source string encoded using utf-8 [STRING]
     * @return string of unicode entities [STRING]
     * @access public
     */
    static function utf2entities($source, $force = false) {
        if (!$force && $GLOBALS['POOL']->config->dbIsUtf8) {
            return $source;
        }
        // array used to figure what number to decrement from character order value
        // according to number of characters used to map unicode to ascii by utf-8
        $decrement[4] = 240;
        $decrement[3] = 224;
        $decrement[2] = 192;
        $decrement[1] = 0;

        // the number of bits to shift each charNum by
        $shift[1][0] = 0;
        $shift[2][0] = 6;
        $shift[2][1] = 0;
        $shift[3][0] = 12;
        $shift[3][1] = 6;
        $shift[3][2] = 0;
        $shift[4][0] = 18;
        $shift[4][1] = 12;
        $shift[4][2] = 6;
        $shift[4][3] = 0;

        $pos = 0;
        $len = strlen($source);
        $encodedString = '';
        while ($pos < $len) {
            $thisLetter = substr($source, $pos, 1);
            $asciiPos = ord($thisLetter);
            $asciiRep = $asciiPos >> 4;

            if ($asciiPos < 128) {
                $pos += 1;
                $thisLen = 1;
            } else
                if ($asciiRep == 12 or $asciiRep == 13) {
                    // 2 chars representing one unicode character
                    $thisLetter = substr($source, $pos, 2);
                    $pos += 2;
                    $thisLen = 2;
                } else
                    if ($asciiRep == 15) {
                        // 4 chars representing one unicode character
                        $thisLetter = substr($source, $pos, 4);
                        $thisLen = 4;
                        $pos += 4;
                    } else
                        if ($asciiRep == 14) {
                            // 3 chars representing one unicode character
                            $thisLetter = substr($source, $pos, 3);
                            $thisLen = 3;
                            $pos += 3;
                        }

            // process the string representing the letter to a unicode entity


            if ($thisLen == 1) {
                $encodedLetter = $thisLetter;
            } else {
                $thisPos = 0;
                $decimalCode = 0;
                while ($thisPos < $thisLen) {
                    $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
                    if ($thisPos == 0) {
                        $charNum = intval($thisCharOrd - $decrement[$thisLen]);
                        $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
                    } else {
                        $charNum = intval($thisCharOrd - 128);
                        $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
                    }

                    $thisPos++;
                }
                if ($decimalCode < 65529) {
                    $encodedLetter = "&#" . $decimalCode . ';';
                } else {
                    $encodedLetter = "";
                }
            }
            $encodedString .= $encodedLetter;

        }
        return $encodedString;
    }

    static function array2query($params) {
        $str = '';
        if (!empty($params)) {
            foreach ($params as $key => $value) {
                $str .= (strlen($str) < 1) ? '' : '&';
                $str .= $key . '=' . rawurlencode($value);
            }
        }
        return ($str);
    }

    static function makeUri($title, $preserveDots = false, $preserveSlashes = false) {
        $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');

        $title = trim($title);
        if (!$title) {
            $title = "none";
        }
        $newValue = $title;
        if (!$preserveDots) {
            $newValue = str_replace(".", "-", $newValue);
        }
        $newValue = str_replace("@", "-at-", $newValue);
        $newValue = preg_replace("/[öÖ]/u", "oe", $newValue);
        $newValue = preg_replace("/[üÜ]/u", "ue", $newValue);
        $newValue = preg_replace("/[äÄ]/u", "ae", $newValue);
        $newValue = preg_replace("/[éèê]/u", "e", $newValue);
        $newValue = preg_replace("/[Ïïíì]/u", "i", $newValue);
        $newValue = preg_replace("/[ñ]/u", "n", $newValue);
        $newValue = preg_replace("/[àåáâ]/u", "a", $newValue);
        $newValue = preg_replace("/[ùú]/u", "u", $newValue);
        $newValue = preg_replace("/[òó]/u", "o", $newValue);
        $newValue = preg_replace("/[ß]/u", "ss", $newValue);

        $newValue = preg_replace("/[\n\r]+/u", "", $newValue);

        //removing everything else
        $newValue = strtolower($newValue);
        $newValue = preg_replace("/[^a-z0-9\.\-\_\/]/", "-", $newValue);

        if (!$preserveDots) {
            $newValue = preg_replace("/_([0-9]+)$/u", "-$1", $newValue);
        } else {
            $newValue = preg_replace("/_([0-9]+)\./u", "-$1.", $newValue);
        }

        if (!$preserveSlashes) {
            $newValue = preg_replace("/\//u", "-$1", $newValue);
        }

        $newValue = preg_replace("/-{2,}/u", "-", $newValue);
        $newValue = trim($newValue, "-");
        if (!$newValue) {
            $newValue = "none";
        }
        return $newValue;
    }

    /* urify is a simple version of the above.
       The reason for having 2 versions is, that the above is not easy to do in XSLT only and
       I don't want to change makeUri 'cause of BC.

       in XSLT, the function is the following:

        <func:function name="bxf:urify">
            <xsl:param name="text"/>
            <func:result select="translate($text,'ABCDEFGHIJKLMNOPQRSTUVWXYZ&#x20;&#x9;&#xA;&#xD;ÄäÜüÖöÏïçÊèÉéÊêÀàÂâÔô_;:\.!,?+$£*ç%&amp;/()=','abcdefghijklmnopqrstuvwxyz----aauuooiiceeeeeeaaaaoo')"/>
        </func:function>
    */
    static function urify($text) {
        $newValue = strtolower(preg_replace("/[_;:\.!,?+$£*ç%&\/\(\)=]/", "", $text));
        $newValue = preg_replace("/[öÖÔô]/u", "o", $newValue);
        $newValue = preg_replace("/[üÜ]/u", "u", $newValue);
        $newValue = preg_replace("/[äÄàÀâ]/u", "a", $newValue);
        $newValue = preg_replace("/[ÊèÉéÊ]/u", "e", $newValue);
        $newValue = preg_replace("/[Ïï]/u", "i", $newValue);
        $newValue = preg_replace("/[ç]/u", "c", $newValue);
        return str_replace(" ", "-", $newValue);
    }

    /**
     * replaces all occurrences of the keys of $textfields in $subject.
     *
     * @param string $subject string containing fieldnames sourrounded by {} which should be replaced
     * @param array $textfields array of key=>value containing the field values
     * @return string string with replaced fields
     * @access public
     */
    function replaceTextFields($subject, $textfields) {
        foreach ($textfields as $field => $value) {
            $field = str_replace('/','\/',$field);
            $patterns[] = '/\{' . $field . '\}/';
            $replacements[] = $value;
        }
        $subject = preg_replace($patterns, $replacements, $subject);
        return $subject;
    }

    /**
     * tidily prints the given fields into a string
     *
     * @param array $fields array of key=>value containing the field values
     * @param boolean $printKey when set to TRUE, the key gets printed as well
     * @return string string with formatted fields
     * @access public
     */
    static function formatTextFields($fields, $printKey = TRUE, $hideFields = array()) {
        $out = '';
        foreach ($fields as $key => $value) {
            if ($printKey) {
                $out .= sprintf('%-20s: ', $key);
            }
            if (strpos($value, "\n") !== false) {
                $value = "\n\n  " . preg_replace("#([\r\n]+)#", "$1  ", $value) . "\n****";
            }
            $out .= "$value\n";
        }
        return $out;
    }

    /**
     * Takes a data array and generates a simple ASCII table out of it according
     * to the given array of definitions.
     *
     * @param array $data Array of arrays containing the data
     * @param array $tableDef Array of array containing the header and column definitions
     * @return string Formatted ASCII table
     * @access public
     */
    static function asciiTable($data, $tableDef) {
        if (empty($data) || !is_array($data)) {
            return '';
        }

        $columnFormats = array();
        $out = '';
        $sep = '';
        $header = '';

        foreach ($tableDef as $i => $column) {
            $cHeading = $column[0];
            $cWidth = $column[1];
            $cOrientation = '-';
            if (isset($column[2]) && strtolower($column[2]) === 'r') {
                $cOrientation = '';
            }

            // resize column if needed
            if ($cWidth < strlen($cHeading)) {
                $cWidth = strlen($cHeading);
            }
            $header .= '|' . sprintf(" %-{$cWidth}.{$cWidth}s ", $column[0]);
            $sep .= '+' . str_repeat('-', $cWidth + 2);

            // save column format for later use
            $columnFormats[$i] = " %{$cOrientation}{$cWidth}.{$cWidth}s ";
        }
        $header .= '|';
        $sep .= '+';

        foreach ($data as $row => $columns) {
            foreach ($columns as $i => $column) {
                $out .= '|' . sprintf($columnFormats[$i], $column);
            }
            $out .= "|\n";
        }

        if ($out != ' ') {
            return $sep . "\n" . $header . "\n" . $sep . "\n" . $out . $sep;
        } else {
            return '';
        }
    }

    /**
     * strips all newlines (\r and \n) from the given string (utf8 save),
     * shortens repeating whitespaces to one character and strips ws from
     * the beginning and the end.
     *
     * @param string $in string to trim
     * @return string trimmed string
     * @access public
     */
    static function trim($in) {
        $in = trim($in);
        $in = preg_replace('/[\s]{2,}/u', ' ', $in);
        $in = preg_replace('/[\r\n]*/u', '', $in);
        return $in;
    }

    static function tidyfy($string) {
        $tidyOptions = array(
                "output-xhtml" => true,
                "show-body-only" => true,
                "clean" => false,
                "wrap" => "0",
                "indent" => false,
                "indent-spaces" => 1,
                "ascii-chars" => false,
                "wrap-attributes" => false,
                "alt-text" => "",
                "doctype" => "loose",
                "numeric-entities" => true,
                "drop-proprietary-attributes" => true);

        if (class_exists("tidy")) {
            $tidy = new tidy();
            if (!$tidy) {
                return $string;
            }
        } else {
            return $string;
        }

        // this preg escapes all not allowed tags...
        $tidy->parseString($string, $tidyOptions, "utf8");
        $tidy->cleanRepair();
        return (string) $tidy;
    }

    static function makeLinksClickable($text) {
        //$res=preg_replace("/((http|ftp)+(s)?:(\/\/)([\w]+(.[\w]+))([\w\-\.,@?^=%&:;\/~\+#]*[\w\-\@?^=%&:;\/~\+#])?)/i", "<a href=\"\\0\">\\0</a>", $text);
        //$res = preg_replace( "#([\s\(\.\:]|\A)(http[s]?:\/\/[^\s^>^<^\)]*)#m", "$1<a href=\"$2\">$2</a>", $text);
        $res = preg_replace("#([\s\(\.\:]|\A)(http[s]?:\/\/[^\s^>^<^\)]*[^\s^>^<^\)^\.^\,])#m", "$1<a href=\"$2\">$2</a>", $text);

        return $res;
    }

    static function removeDoubleSlashes($str) {
        return preg_replace("#\/{2,}#", "/", $str);
    }

    static function spacesToPlus($str) {
        return str_replace(' ', '+', $str);
    }

    static function escapeJSValue($str) {
        return str_replace(array(
                "'",
                "\n"), array(
                "\\'",
                "\\n"), $str);
    }

    public static function isUtf8($string) {
        // From http://w3.org/International/questions/qa-forms-utf-8.html
        return preg_match('%^(?:
              [\x09\x0A\x0D\x20-\x7E]            # ASCII
            | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
            |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
            | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
            |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
            |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
            | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
            |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
            )*$%xs', $string);
    }

    public static function isISO88591($str) {
        return preg_match('/^([\x09\x0A\x0D\x20-\x7E\xA0-\xFF])*$/', $str);
    }

    static function transformFromContentTypeToUTF8($str) {

        if (isset($_SERVER['CONTENT_TYPE']) && preg_match('#charset=([^/s^;]+)#', $_SERVER['CONTENT_TYPE'], $matches)) {
            if ($matches[1] == 'UTF-8') {
                return $str;
            }
            if ($matches[1] == "ISO-8859-1") {
                return utf8_encode($str);
            }
            return iconv($matches[1], "UTF-8", $str);
        }
        //if no charset, then return as it came
        return $str;
    }

    static function fixXMLEncodingFromHTTP($xml) {
        if (!preg_match("#<?xml[^>]+encoding=#", $xml)) {
            return bx_helpers_string::transformFromContentTypeToUTF8($xml);
        }
        return $xml;
    }

}
Back to Top