Frage

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.

War es hilfreich?

Lösung

Instead of this

/#(\w+)/

use this

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

to make sure that the first character is a letter.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top