Pergunta

I'm using the code

function twitterify($ret) {
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
    $ret = preg_replace("/#(\w+)/", "<a href=\"http://twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
    return $ret;
}

to parse text from Tweets and add links. If #1 appears in the Tweet text then a hashtag link will be created for it. That's incorrect since hashtags can't start with numbers.

How can the regular expressions be modified to correct this?

Also special characters should be excluded according to this.

Foi útil?

Solução

Instead of this

/#(\w+)/

use this

/#([a-zA-Z]\w+)/

to make sure that the first character is a letter.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top