开发者

preg_replace and twitter

i have this code to make clickable links into my app:

$string = preg_replace('!(((f|ht)tp://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1" rel="nofollow">$1</a>', $string);
$string = preg_replace('/@(\w+)/', '<a href="http://twitter.com/$1" rel="nofollow">$0</a>', $string);
$string = preg_replace('/#(\w+)/', '<a href="http://search.twitter.com/search?q=%23$1" rel="nofollow">$0</a>', $string);

Above code is working fine, but suddenly Im seeing some links like http://twitter.com/#!/username and http://domain.com/hello@all/ and breaks everything, any idea how to fix my code?

string var c开发者_StackOverflow社区omes directly from twitter API, here is an example: $string = 'http://twitter.com/!#/metallica http://someurl.com/get@stuff/';

Thanks in advance. Edited: added string value.


Why are you tampering with those URL? If they are direct links, then just leave them as they are! Simple regex pattern like this:

((?:f|ht)tp://[^\s]*)

will match any URL inside the string.

So, your code should be just:

$string = preg_replace('/((?:f|ht)tp:\/\/[^\s]*)/i', '<a href="$1" rel="nofollow">$1</a>', $string);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜