3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Content\Text;
33 * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
35 public static function shorten($msg, $limit)
37 $lines = explode("\n", $msg);
39 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
40 $ellipsis = html_entity_decode("…", 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;
56 * Returns the character positions of the provided boundaries, optionally skipping a number of first occurrences
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
64 public static function getBoundariesPosition($text, $open, $close, $occurrences = 0)
66 if ($occurrences < 0) {
71 for ($i = 0; $i <= $occurrences; $i++) {
72 if ($start_pos !== false) {
73 $start_pos = strpos($text, $open, $start_pos + 1);
77 if ($start_pos === false) {
81 $end_pos = strpos($text, $close, $start_pos);
83 if ($end_pos === false) {
87 $res = ['start' => $start_pos, 'end' => $end_pos];