문제

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.

도움이 되었습니까?

해결책

Instead of this

/#(\w+)/

use this

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

to make sure that the first character is a letter.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top