]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/Plaintext.php
Merge pull request #8980 from annando/fcontact-model
[friendica.git] / src / Content / Text / Plaintext.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Content\Text;
23
24 class Plaintext
25 {
26         /**
27          * Shortens message
28          *
29          * @param  string $msg
30          * @param  int    $limit
31          * @return string
32          *
33          * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
34          */
35         public static function shorten($msg, $limit)
36         {
37                 $lines = explode("\n", $msg);
38                 $msg = "";
39                 $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
40                 $ellipsis = html_entity_decode("&#x2026;", ENT_QUOTES, 'UTF-8');
41                 foreach ($lines as $row => $line) {
42                         if (iconv_strlen(trim($msg . "\n" . $line), "UTF-8") <= $limit) {
43                                 $msg = trim($msg . "\n" . $line);
44                         } elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
45                                 // Is the new message empty by now or is it a reshared message?
46                                 $msg = iconv_substr(iconv_substr(trim($msg . "\n" . $line), 0, $limit, "UTF-8"), 0, -3, "UTF-8") . $ellipsis;
47                         } else {
48                                 break;
49                         }
50                 }
51
52                 return $msg;
53         }
54
55         /**
56          * Returns the character positions of the provided boundaries, optionally skipping a number of first occurrences
57          *
58          * @param string $text        Text to search
59          * @param string $open        Left boundary
60          * @param string $close       Right boundary
61          * @param int    $occurrences Number of first occurrences to skip
62          * @return boolean|array
63          */
64         public static function getBoundariesPosition($text, $open, $close, $occurrences = 0)
65         {
66                 if ($occurrences < 0) {
67                         $occurrences = 0;
68                 }
69
70                 $start_pos = -1;
71                 for ($i = 0; $i <= $occurrences; $i++) {
72                         if ($start_pos !== false) {
73                                 $start_pos = strpos($text, $open, $start_pos + 1);
74                         }
75                 }
76
77                 if ($start_pos === false) {
78                         return false;
79                 }
80
81                 $end_pos = strpos($text, $close, $start_pos);
82
83                 if ($end_pos === false) {
84                         return false;
85                 }
86
87                 $res = ['start' => $start_pos, 'end' => $end_pos];
88
89                 return $res;
90         }
91 }