+ /**
+ * Converts text into a format that can be used for the channel search and the language detection.
+ *
+ * @param string $text
+ * @param integer $uri_id
+ * @return string
+ */
+ public static function toSearchText(string $text, int $uri_id): string
+ {
+ // Removes attachments
+ $text = self::removeAttachment($text);
+
+ // Add images because of possible alt texts
+ if (!empty($uri_id)) {
+ $text = Post\Media::addAttachmentsToBody($uri_id, $text, [Post\Media::IMAGE]);
+ }
+
+ if (empty($text)) {
+ return '';
+ }
+
+ // Remove links without a link description
+ $text = preg_replace("~\[url\=.*\]https?:.*\[\/url\]~", ' ', $text);
+
+ // Remove pictures
+ $text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", ' ', $text);
+
+ // Replace picture with the alt description
+ $text = preg_replace("/\[img\=.*?\](.*?)\[\/img\]/ism", ' $1 ', $text);
+
+ // Remove the other pictures
+ $text = preg_replace("/\[img.*?\[\/img\]/ism", ' ', $text);
+
+ // Removes mentions, remove links from hashtags
+ $text = preg_replace('/[@!]\[url\=.*?\].*?\[\/url\]/ism', ' ', $text);
+ $text = preg_replace('/[#]\[url\=.*?\](.*?)\[\/url\]/ism', ' #$1 ', $text);
+ $text = preg_replace('/[@!#]?\[url.*?\[\/url\]/ism', ' ', $text);
+ $text = preg_replace("/\[url=[^\[\]]*\](.*)\[\/url\]/Usi", ' $1 ', $text);
+
+ // Convert it to plain text
+ $text = self::toPlaintext($text, false);
+
+ // Remove possibly remaining links
+ $text = preg_replace(Strings::autoLinkRegEx(), '', $text);
+
+ // Remove all unneeded white space
+ do {
+ $oldtext = $text;
+ $text = str_replace([' ', "\n", "\r", '"'], ' ', $text);
+ } while ($oldtext != $text);
+
+ return trim($text);
+ }
+