Skip to content

Instantly share code, notes, and snippets.

@shukob
Last active May 9, 2018 07:39
Show Gist options
  • Save shukob/fe755b18e0d90867b786f1b7ab275665 to your computer and use it in GitHub Desktop.
Save shukob/fe755b18e0d90867b786f1b7ab275665 to your computer and use it in GitHub Desktop.
Hashtag to link in humhub
public static function convertHashtags($text)
{
return preg_replace_callback(
'/#(?!039)([\w]+)/', // all "word" characters, all digits, and underscore
// brackets around the string AFTER the hashtag
function ($hit) {
// $matches[0] is the complete match (including the hashtag)
// $matches[1] is the match for the subpattern enclosed in brackets
return '[' . $hit[0] . '](' . 'http://localhost:8080/index.php?r=' . Html::encode('search/search/index') .
'&' . Html::encode('SearchForm[keyword]') . '=' . strtolower($hit[1]) . ')';
}, $text);
}
public function run()
{
$this->originalText = $this->text;
if ($this->encode) {
$this->text = Html::encode($this->text);
}
if (!$this->minimal && !$this->edit) {
$this->translateOmbed();
// mark emails
$this->text = preg_replace_callback('/[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})/', function ($match) {
return Html::mailto($match[0]);
}, $this->text);
}
$this->text = self::convertHashtags($this->text);
// get user and space details from guids
....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment