diff options
| author | horus | 2024-01-16 17:56:14 +0100 |
|---|---|---|
| committer | horus | 2024-01-16 17:56:14 +0100 |
| commit | 94baf5917979db7e06f779844d1dbab6c2936bcc (patch) | |
| tree | 535fb0d2991a8369cb78751b7198d76b47d203a7 /app/Libraries/Helper.php | |
| parent | 870c335022a43b2cb321a5c436b37981a61d2a46 (diff) | |
| download | curious-94baf5917979db7e06f779844d1dbab6c2936bcc.tar.gz | |
minor improvements with hashtag handling
Diffstat (limited to 'app/Libraries/Helper.php')
| -rw-r--r-- | app/Libraries/Helper.php | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/app/Libraries/Helper.php b/app/Libraries/Helper.php index 311c466..026abef 100644 --- a/app/Libraries/Helper.php +++ b/app/Libraries/Helper.php @@ -73,11 +73,38 @@ class Helper { if ( "mastodon" == $title ) { $cat_len= 0; + if ( ! $post->getCategories()->get()->isEmpty() ) { - $categories = "#MostDiscussed "; + + $categories_ar = array("#MostDiscussed"); + foreach( $post->getCategories()->get() as $cat ) { - $categories .= "#". $cat->name . " "; + + // uppercase for every word in a possible multi worded hashtag + $tmp_cat = ucwords($cat->name); + + // strip everything after / for brevity + if ( false !== strpos($tmp_cat, "/") ) + $tmp_cat = substr($tmp_cat, 0, strpos($tmp_cat, "/")); + + // replace any non-alphanumeric character except underscore, because + // it's not allowed to be used in a hashtag + $tmp_cat = preg_replace("/[^A-Za-z0-9_]/", '', $tmp_cat); + + // trim just in case + $tmp_cat = trim($tmp_cat); + + // if it's not empty, add it to the hashtag array + if ( "" != $tmp_cat ) + $categories_ar[] = "#". $tmp_cat; } + + // remove possible duplicates (because stripping after "/") + $categories_ar = array_unique($categories_ar); + + // join to one string + $categories = implode(" ", $categories_ar); + $cat_counter = $post->getCategories()->get()->count(); $cat_len = mb_strlen($categories, "UTF-8"); } |
