]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/Plaintext.php
Merge pull request #4399 from MrPetovan/task/3878-move-include-bbcode-to-src
[friendica.git] / src / Content / Text / Plaintext.php
1 <?php
2 /**
3  * @file src/Content/Text/Plaintext.php
4  */
5 namespace Friendica\Content\Text;
6
7 class Plaintext
8 {
9         /**
10          * Shortens message
11          *
12          * @param type $msg
13          * @param type $limit
14          * @return type
15          *
16          * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
17          */
18         public static function shorten($msg, $limit)
19         {
20                 $lines = explode("\n", $msg);
21                 $msg = "";
22                 $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
23                 $ellipsis = html_entity_decode("&#x2026;", ENT_QUOTES, 'UTF-8');
24                 foreach ($lines as $row => $line) {
25                         if (iconv_strlen(trim($msg . "\n" . $line), "UTF-8") <= $limit) {
26                                 $msg = trim($msg . "\n" . $line);
27                         } elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
28                                 // Is the new message empty by now or is it a reshared message?
29                                 $msg = iconv_substr(iconv_substr(trim($msg . "\n" . $line), 0, $limit, "UTF-8"), 0, -3, "UTF-8") . $ellipsis;
30                         } else {
31                                 break;
32                         }
33                 }
34
35                 return $msg;
36         }
37
38         /**
39          * Returns the character positions of the provided boundaries, optionally skipping a number of first occurrences
40          *
41          * @param string $text        Text to search
42          * @param string $open        Left boundary
43          * @param string $close       Right boundary
44          * @param int    $occurrences Number of first occurrences to skip
45          * @return boolean|array
46          */
47         public static function getBoundariesPosition($text, $open, $close, $occurrences = 0)
48         {
49                 if ($occurrences < 0) {
50                         $occurrences = 0;
51                 }
52
53                 $start_pos = -1;
54                 for ($i = 0; $i <= $occurrences; $i++) {
55                         if ($start_pos !== false) {
56                                 $start_pos = strpos($text, $open, $start_pos + 1);
57                         }
58                 }
59
60                 if ($start_pos === false) {
61                         return false;
62                 }
63
64                 $end_pos = strpos($text, $close, $start_pos);
65
66                 if ($end_pos === false) {
67                         return false;
68                 }
69
70                 $res = ['start' => $start_pos, 'end' => $end_pos];
71
72                 return $res;
73         }
74 }