src/String/StringTools.php
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;
    }
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;
    }
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 ; }
Detects HashTags in a string and calls a callback function for each. Default behaviour is to insert HTML links for each HashTag.

@param string $text  String to find HashTags in
@param callable|null $formatHashTag  Will be called for each HashTag detected. If null, default behaviour will happen (inserting a link for each HashTag)
@param string $url  If $formatHashTag is not set, this will prefix any linked HashTag
@param bool $openLinksInNewWindow  If $formatHashTag is not set, created links will open in new browser window
@return string
Detects HashTags in a string and calls a callback function for each . Default behaviour is to insert HTML links for each HashTag .