- $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui';
- $replacement = '$1<a href="'. $indexUrl .'./add-tag/$2" title="Hashtag $2">#$2</a>';
- return preg_replace($regex, $replacement, $description);
+ $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}' . $tokens . ']+)/mui';
+ $format = function (array $match) use ($indexUrl): string {
+ $cleanMatch = str_replace(
+ BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_OPEN,
+ '',
+ str_replace(BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_CLOSE, '', $match[2])
+ );
+ return $match[1] . '<a href="' . $indexUrl . './add-tag/' . $cleanMatch . '"' .
+ ' title="Hashtag ' . $cleanMatch . '">' .
+ '#' . $match[2] .
+ '</a>';
+ };
+
+ return preg_replace_callback($regex, $format, $description);