dongyixun0634 2013-12-27 09:14
浏览 38
已采纳

PHP中的正则表达式将用户名更改为漂亮的链接

I am using a PHP regular expression to change usernames to pretty links in text.
Firstly, It looks for an @ sign,
Secondly, It checks if the @ sign has a user id touching it, E.G. @User10,
Thirdly, It makes sure that the user id is between 1 and 20 characters,
Fourthly it makes the links pretty.

This is my Regex

/(?<=^|\s|[([])\@([a-zA-Z0-9_]{1,20})/

This regex works fine for these text examples

@User1 This is a test string @User2
@User3

But it does not work in these text examples

<br>@User1 Hello John@User2

All of these Users should be matched but they are not being matched if a string is touching the @ sign before it.

I have an online regex example set here. http://regex101.com/r/zK9kC4

  • 写回答

4条回答 默认 最新

  • dongyou2305 2013-12-27 09:24
    关注

    I tried this expression on http://gskinner.com/RegExr/

    /@([a-zA-Z0-9_]{1,20})/
    

    It matches all criteria you have given for the User-Id's.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部