Add URl links and better hashtag support
rodzic
00a78af84c
commit
cddbe3e1c5
14
index.php
14
index.php
|
@ -7,6 +7,7 @@
|
||||||
* For educational purposes only.
|
* For educational purposes only.
|
||||||
* It produces an Actor who can be followed.
|
* It produces an Actor who can be followed.
|
||||||
* It can send messages to followers.
|
* It can send messages to followers.
|
||||||
|
* Those messages can have linkable URls and hashtags.
|
||||||
* It saves logs about requests it receives and sends.
|
* It saves logs about requests it receives and sends.
|
||||||
* It is NOT suitable for production use.
|
* It is NOT suitable for production use.
|
||||||
* This code is "licenced" under CRAPL v0 - https://matt.might.net/articles/crapl/
|
* 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.
|
// 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 ) {
|
function process_content( $content ) {
|
||||||
global $server;
|
global $server;
|
||||||
|
|
||||||
|
// Convert any URls into hyperlinks
|
||||||
|
$link_pattern = '/\bhttps?:\/\/\S+/iu';
|
||||||
|
$replacement = function ( $match ) {
|
||||||
|
$url = htmlspecialchars( $match[0], ENT_QUOTES, "UTF-8" );
|
||||||
|
return "<a href=\"$url\">$url</a>";
|
||||||
|
};
|
||||||
|
$content = preg_replace_callback( $link_pattern, $replacement, $content );
|
||||||
|
|
||||||
// Get any hashtags
|
// Get any hashtags
|
||||||
$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) {
|
foreach ($matches[1] as $match) {
|
||||||
$hashtags[] = $match;
|
$hashtags[] = $match;
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue