From e50aa27eebdef84e34ca090fdaebfc66489d86c8 Mon Sep 17 00:00:00 2001 From: Alma Armas Date: Mon, 24 Aug 2020 11:37:47 +0000 Subject: [PATCH] Fix hyperlinker --- Web/Models/Entities/Traits/TRichText.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Web/Models/Entities/Traits/TRichText.php b/Web/Models/Entities/Traits/TRichText.php index 6bb7f304..faec0bca 100644 --- a/Web/Models/Entities/Traits/TRichText.php +++ b/Web/Models/Entities/Traits/TRichText.php @@ -26,6 +26,21 @@ trait TRichText return $text; } + + private function formatLinks(string &$text): string + { + return preg_replace_callback( + "%(([A-z]++):\/\/(\S*?\.\S*?))([\s)\[\]{},;\"\'<]|\.\s|$)%", + (function (array $matches): string { + $href = str_replace("#", "#", $matches[1]); + $link = str_replace("#", "#", $matches[3]); + $rel = $this->isAd() ? "sponsored" : "ugc"; + + return "$link" . htmlentities($matches[4]); + }), + $text + ); + } private function removeZalgo(string $text): string { @@ -39,11 +54,7 @@ trait TRichText if($html) { if($proc) { $rel = $this->isAd() ? "sponsored" : "ugc"; - $text = preg_replace( - "%((https?|ftp):\/\/(\S*?\.\S*?))([\s)\[\]{},;\"\':<]|\.\s|$)%", - "$3$4", - $text - ); + $text = $this->formatLinks($text); $text = preg_replace("%@(id|club)([0-9]++) \(([\p{L} 0-9]+)\)%Xu", "[$1$2|$3]", $text); $text = preg_replace("%@(id|club)([0-9]++)%Xu", "[$1$2|@$1$2]", $text); $text = preg_replace("%\[(id|club)([0-9]++)\|([\p{L} 0-9@]+)\]%Xu", "$3", $text);