2010-10-25 35 views
1

我用魔杖可以用[USER] USERNAME [/ user] BBCode替换www.example.com/profile/USERNAME这样的URL。正则表达式 - 负向预测声明

$userURLSearch = "#((https?|ftp)://|www\.)example\.com/profile/([A-Za-z][A-Za-z0-9_-]+)(?!/)#i"; 
$userURLReplace = "[user]\\3[/user]"; 
$text = preg_replace($userURLSearch, $userURLReplace, $text); 

但它也像变换网址www.example.com/profile/USERNAME/more/and/more进入[用户] USERNAME [/ USER] /更/和/更多... :( 它应该只变换正是 “/型材/ USERNAME”。

有人可以帮我吗? THX

回答

1

更换(?!/)由$在结束了吗?

+0

这只会匹配结束的字符串的URL 。$ text可能包含很多需要替换的URL ... – thetaiko 2010-10-25 16:29:26

+0

哦,对,我不明白这一点。所以你想要它后面跟着一个不是'/'的非单词字符?将(b!)放在(?!/)之前呢? – 2010-10-25 16:35:44

+0

Thx!科林Fine的第二个答案完美:) – Xolf 2010-10-25 16:44:28