2011-04-17 29 views
0

我有這樣的代碼,以使可點擊的鏈接到我的應用程序:的preg_replace和Twitter

$string = preg_replace('!(((f|ht)tp://)[-a-zA-Zа-яА-Я()[email protected]:%_+.~#?&;//=]+)!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); 

上面的代碼工作正常,但突然間林看到這樣http://twitter.com/#!/usernamehttp://domain.com/[email protected]/一些鏈接,打破一切,任何知道如何解決我的代碼?

串VAR直接來自Twitter的API,這裏有一個例子:$string = 'http://twitter.com/!#/metallica http://someurl.com/[email protected]/ ';

在此先感謝。 已編輯:添加字符串值。

+0

嘗試'(((F | HT)TP://)[-a-ZA-Zа-яА-Я()0-9 @ :%_ +。〜#?&; // =!*] +)!i'作爲您的正則表達式。 – erisco 2011-04-17 04:48:17

+0

你可以提出'$ string'的值嗎? – 2011-04-17 04:49:59

+0

@erisco謝謝,但我得到這個錯誤:未知修飾符'*'/ @ experimentX你是什麼意思? – greenbandit 2011-04-17 04:51:39

回答

2

你爲什麼這些網址被篡改?如果他們是直接聯繫,那就讓他們保持原樣吧!像這樣簡單的正則表達式:

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

將匹配字符串中的任何URL。

所以,你的代碼應該只是:

​​