preg_replace 也删除空格
我正在使用此代码来使用“标记”系统,例如 Twitter 具有:@username,它将链接到他们的个人资料
preg_replace("/@(\w+)/", '<a href="http://www.buddyweb.me/profile/?username=$1">@$1</a>', ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"http://assets.buddyweb.me/scripts/run_link.php?url=\\0\" target=\"_blank\" class = \"user_created_link\" title = \"External Link\">\\0</a>", $original));
它可以找到,但如果有两个彼此相邻,例如:
@someone @else< /code>
它将它们链接到配置文件,但它删除了它们之间的空格,例如:
@someone@else
我知道这很简单,但我对 PHP 有点陌生,所以我不会'真的不知道要删除什么,我尝试从上面的代码中删除 /
,但这破坏了整个事情
编辑
我已经发布了完整的代码。
I am using this code to use a "tagging" system, like twitter has: @username and it will link up to their profile
preg_replace("/@(\w+)/", '<a href="http://www.buddyweb.me/profile/?username=$1">@$1</a>', ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"http://assets.buddyweb.me/scripts/run_link.php?url=\\0\" target=\"_blank\" class = \"user_created_link\" title = \"External Link\">\\0</a>", $original));
It works find but if there are two next to each other, e.g:
@someone @else
it will link them up to the profile, but it removes the space between them, e.g:
@someone@else
I know it's something easy but I'm kinda new to PHP so I wouldn't really know what to remove, I've tried removing the /
's from the above code but that ruins the whole thing
EDIT
I have published the full code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你可以在锚标记后添加一个空格:
i would think you can add a space after the anchor tag:
奇怪的是,它确实链接了所有@username,因为您没有在正则表达式末尾指定 g 修饰符。 g 修饰符将替换所有出现的情况。 Lbu 的答案实际上应该是添加 1 个额外的空格,因此您应该得到 2 个空格。
一个很棒的练习网站是 http://gskinner.com/RegExr/ 祝你好运!:)
It's strange that it does link all @usernames cause you didn't specify the g modifier at the end of the regular expression. The g modifier will replace all occurences. Lbu's answer should actually be adding 1 extra space so you should end up with 2 spaces.
A great website to practise with is http://gskinner.com/RegExr/ Good luck!:)