From cddbe3e1c5dd797ce1e1475ca7063256afe6aa98 Mon Sep 17 00:00:00 2001 From: Terence Eden Date: Sat, 17 Feb 2024 09:30:50 +0000 Subject: [PATCH] Add URl links and better hashtag support --- index.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index ff8e73b..dcc4d34 100644 --- a/index.php +++ b/index.php @@ -7,6 +7,7 @@ * For educational purposes only. * It produces an Actor who can be followed. * It can send messages to followers. + * Those messages can have linkable URls and hashtags. * It saves logs about requests it receives and sends. * It is NOT suitable for production use. * This code is "licenced" under CRAPL v0 - https://matt.might.net/articles/crapl/ @@ -467,13 +468,22 @@ HTML; } // Content can be plain text. But to add clickable links and hashtags, it needs to be turned into HTML. - // Tags are also included separately in the message + // Tags are also included separately in the note function process_content( $content ) { global $server; + // Convert any URls into hyperlinks + $link_pattern = '/\bhttps?:\/\/\S+/iu'; + $replacement = function ( $match ) { + $url = htmlspecialchars( $match[0], ENT_QUOTES, "UTF-8" ); + return "$url"; + }; + $content = preg_replace_callback( $link_pattern, $replacement, $content ); + // Get any hashtags $hashtags = []; - preg_match_all( '/#(\w+)/', $content, $matches ); + $hashtag_pattern = '/(?:^|\s)\#(\w+)/'; // Beginning of string, or whitespace, followed by # + preg_match_all( $hashtag_pattern, $content, $matches ); foreach ($matches[1] as $match) { $hashtags[] = $match; }