public static function detectHashTags($text, callable $formatHashTag = null, $url = '', $openLinksInNewWindow = true)
    {
        if ($formatHashTag == null) {
            $formatHashTag = function ($hashTag) use ($url, $openLinksInNewWindow) {
                return '<a href="' . $url . urlencode($hashTag) . ($openLinksInNewWindow ? ' target="_blank"' : '') . '">' . $hashTag . '</a>';
            };
        }

        $hashTagRegex = '/(^|\s)(#\w+)($|[^<])/';
        while (preg_match($hashTagRegex, $text, $match, PREG_OFFSET_CAPTURE)) {
            list($hashTag, $index) = $match[2];
            $text = substr($text, 0, $index) . $formatHashTag($hashTag) . strpos($text, $index + strlen($hashTag));
        }

        return $text;
    }